122. Best Time to Buy and Sell Stock II
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
The idea is 低买高卖, buy in the valley and sell at the peak.
Idea
- Start from the beginning, loop throught the prices array
- Find the next valley and sell at the next peak
The idea can be simplified to the following code.
int maxProfit(vector<int> &prices) {
int maxprofit = 0;
for (int i = 1; i < prices.size(); i++)
maxprofit += max(prices[i] - prices[i - 1], 0);
return maxprofit;
}
While looking for the next valley, 0 is always added to maxprofit
.
While looking for the next peak, it will accumulate the differences.
Time Complexity:
Space Complexity: