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; }
相关文章推荐
- LeetCode(122) Best Time to Buy and Sell Stock II (Java)
- leetcode || 122、Best Time to Buy and Sell Stock II
- leetcode 122 —— Best Time to Buy and Sell Stock II
- leetcode: (122) Best Time to Buy and Sell Stock II
- [Leetcode 122, Medium] Best Time to Buy and Sell Stock II
- LeetCode 122: Best Time to Buy and Sell Stock II
- LeetCode: Best Time to Buy and Sell Stock II [122]
- 122 Best Time to Buy and Sell Stock II [Leetcode]
- leetcode 122: Best Time to Buy and Sell Stock II
- LeetCode122:Best Time to Buy and Sell Stock II
- Leetcode_122_Best Time to Buy and Sell Stock II
- LeetCode122:Best Time to Buy and Sell Stock II
- [Leetcode 51] 122 Best Time to Buy and Sell Stock II
- [LeetCode] 122 - Best Time to Buy and Sell Stock II
- LeetCode: Best Time to Buy and Sell Stock II [122]
- LeetCode 122: Best Time to Buy and Sell Stock II
- leetcode-122 Best Time to Buy and Sell Stock II
- leetcode_question_122 Best Time to Buy and Sell Stock II
- LeetCode 122 Best Time to Buy and Sell Stock II
- [LeetCode 122] - 买入与卖出股票的最佳时机II(Best Time to Buy and Sell Stock II)