Best Time to Buy and Sell Stock with Cooldown LeetCode solution in Java
1class Solution {2public int maxProfit(int[] prices) {34int n = prices.length;56int[][] dp = new int[n + 2][2];78for (int index = n - 1; index >= 0; index--) {9for (int buy = 0; buy <= 1; buy++) {1011int profit = 0;1213if (buy == 0) { // buy stocks14profit = Math.max(-prices[index] + dp[index + 1][1], 0 + dp[index + 1][0]);15}16if (buy == 1) { // we can sell stocks17profit = Math.max(prices[index] + dp[index + 2][0], 0 + dp[index + 1][1]);18}19dp[index][buy] = profit;20}21}22return dp[0][0];23}24}