Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int maxProfit(const vector<int>& prices) {
- if (prices.empty()) return 0;
- auto memo = vector<int>(prices.size(), -1);
- memo.back() = 0;
- for (int i = prices.size() - 2; i >= 0; --i) {
- int res = memo[i + 1]; // do not sell (and do not buy)
- for (int j = i + 1; j < prices.size(); ++j) { // j is day to buy
- auto suffix_res = j + 2 < memo.size() ? memo[j + 2] : 0;
- res = max(res, prices[j] - prices[i] + suffix_res); // sell
- }
- memo[i] = res;
- }
- return memo[0];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement