码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode#122 Best Time to Buy and Sell Stock II

时间:2015-01-30 16:54:46      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

原题地址

 

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

 

代码:

 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 }

 

Leetcode#122 Best Time to Buy and Sell Stock II

标签:

原文地址:http://www.cnblogs.com/boring09/p/4262487.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!