原题地址

 

如果不限交易次数,把所有递增序列差值求和即可。

 

代码:

 1 int maxProfit(vector<int> &prices) {
 2         if (prices.empty()) 
 3             return 0;
 4         
 5         int profit = 0;
 6         int climax = prices[prices.size() - 1];
 7         
 8         for (int i = prices.size() - 2; i >= 0; i--) {
 9             if (prices[i] >= prices[i + 1]) {
10                 profit += climax - prices[i + 1];
11                 climax = prices[i];
12             }
13         }
14         profit += climax - prices[0];
15         
16         return profit;
17 }

 

相关文章:

  • 2022-01-19
  • 2021-11-19
  • 2022-12-23
  • 2022-12-23
  • 2021-04-14
猜你喜欢
  • 2021-08-02
  • 2021-07-27
  • 2021-10-12
  • 2021-04-13
  • 2021-07-11
  • 2021-05-17
  • 2021-11-06
  • 2021-08-17
相关资源
相似解决方案