class Solution {
public:
int maxProfit(vector<int>& prices) {
int state1 = 0;
int state2 = -prices[0];
for (int i = 1; i < prices.size(); i++) {
state1 = max(state1, state2 + prices[i]);
state2 = max(state2, state1 - prices[i]);
}
return state1;
}
};
Time Complexity: O(n)
Space: O(1)
經典 DP 題