class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
vector<pair<int, int>> dp(n, {0, 0});
dp[0].first = 0;
dp[0].second = -prices[0];
for (int i = 1; i < n; ++i) {
dp[i].first = max(dp[i - 1].first, dp[i - 1].second + prices[i]);
dp[i].second = max(dp[i - 1].second, dp[i - 1].first - prices[i]);
}
return dp[n - 1].first;
}
};
122. 买卖股票的最佳时机 II
最新推荐文章于 2024-08-15 05:02:54 发布