diff --git a/java/309-Best-Time-To-Buy-and-Sell-Stock-With-Cooldown.java b/java/309-Best-Time-To-Buy-and-Sell-Stock-With-Cooldown.java new file mode 100644 index 000000000..1cacc99df --- /dev/null +++ b/java/309-Best-Time-To-Buy-and-Sell-Stock-With-Cooldown.java @@ -0,0 +1,29 @@ +class Solution { + public int maxProfit(int[] prices) { + Map cache = new HashMap<>(); + return dfs(prices, cache, 0, true); + } + + public int dfs(int[] prices, Map cache, int index, boolean buying) { + if (index >= prices.length) { + return 0; + } + String key = index+"-"+buying; + + if (cache.containsKey(key)) { + return cache.get(key); + } + + int cooldown = dfs(prices, cache, index + 1, buying); + int buyOsell = Integer.MIN_VALUE; + + if (buying) { + buyOsell = dfs(prices, cache, index + 1, !buying) - prices[index]; + } else { + buyOsell = dfs(prices, cache, index + 2, !buying) + prices[index]; + } + + cache.put(key, Math.max(buyOsell, cooldown)); + return cache.get(key); + } +} \ No newline at end of file