Add to List 121. Best Time to Buy and Sell Stock

Posted bloomingFlower

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Add to List 121. Best Time to Buy and Sell Stock相关的知识,希望对你有一定的参考价值。

class Solution {
public:
int maxProfit(vector<int>& prices) {
int maxCur = 0, maxAll = 0;
if(prices.size() <= 1) return 0;
auto i = prices.begin();
++i;
for(; i != prices.end(); ++i) {
maxCur = max(0, maxCur += *i - *(--i));
maxAll = max(maxCur, maxAll);
++i;
}
return maxAll;
}
};

以上是关于Add to List 121. Best Time to Buy and Sell Stock的主要内容,如果未能解决你的问题,请参考以下文章

121. Best Time to Buy and Sell Stock

121. Best Time to Buy and Sell Stock

算法分析与设计第一周121.Best Time to Buy and Sell Stock&122. Best Time to Buy and Sell Stock II

121. Best Time to Buy and Sell Stock

121. Best Time to Buy and Sell Stock

121. Best Time to Buy and Sell Stockeasy