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.

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int> &prices) {
 4         // Note: The Solution object is instantiated only once and is reused by each test case.
 5         int max_profit = 0;
 6         for(int i = 0; i < prices.size(); i ++) {
 7             if(i + 1 >= prices.size()) 
 8                 break;
 9                 
10             if(prices[i + 1] > prices[i])
11                 max_profit +=  prices[i + 1] - prices[i];
12         }
13         
14         return max_profit;
15     }
16 };

相关文章:

  • 2021-06-13
  • 2022-03-07
  • 2021-08-10
  • 2022-01-16
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-03
  • 2022-02-17
  • 2021-09-02
  • 2022-02-08
  • 2021-06-06
  • 2021-07-14
相关资源
相似解决方案