Best Time to Buy and Sell Stock with Cooldown LeetCode solution in C++
1class Solution {2public:3int maxProfit(vector<int> &prices) {4int n = prices.size();5int ans = 0;6vector<int> dp(n, 0);7for (int i = 1; i < n; ++i) {8int max_dp = 0;9for (int j = 0; j < i; ++j) {10dp[i] = max(dp[i], prices[i] - prices[j] + max_dp);11max_dp = max(max_dp, j > 0 ? dp[j - 1] : 0);12}13ans = max(ans, dp[i]);14}15return ans;16}17};