Best Time to Buy and Sell Stock with Cooldown LeetCode solution in C++

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

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0