Best Time to Buy and Sell Stock II [LeetCode]

时间:2023-03-08 21:30:34

Problem Description: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/

Basic idea: from code below, it seems super easy. But intuitively, we may use one more variable "tmp_max_profit" to record every times we get the max profit.

 class Solution {
public:
int maxProfit(vector<int> &prices) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int max_profit = ;
for(int i = ; i < prices.size(); i ++) {
if(i + >= prices.size())
break; if(prices[i + ] > prices[i])
max_profit += prices[i + ] - prices[i];
} return max_profit;
}
};