您的位置:首页 > 其它

Leetcode#122 Best Time to Buy and Sell Stock II

2015-01-30 15:34 423 查看
原题地址

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

代码:

int maxProfit(vector<int> &prices) {
if (prices.empty())
return 0;

int profit = 0;
int climax = prices[prices.size() - 1];

for (int i = prices.size() - 2; i >= 0; i--) {
if (prices[i] >= prices[i + 1]) {
profit += climax - prices[i + 1];
climax = prices[i];
}
}
profit += climax - prices[0];

return profit;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: