Leetcode 122 Best Time to Buy and Sell Stock II
2017-10-03 11:16
435 查看
Leetcode 122 Best Time to Buy and Sell Stock II
/* * 一有获利空间就进行买卖 * 也可以理解成,在递减序列的最后一个点买入,然后在递增序列的第一个点卖出 * 例:1 3 5 4 3 1 6 7 8 * maxprofit = 3-1 + 5-3 + 6-1 +7-6 + 8-7 */ //为121题的思路所局限,想复杂了…… class Solution { public: int maxProfit(vector<int>& prices) { int maxprofit = 0; for(int i = 1;i < prices.size();i ++) { if(prices[i] > prices[i - 1]) { int diff = prices[i] - prices[i-1]; maxprofit += diff; } } return maxprofit; } };
相关文章推荐
- leetcode122~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_[python/C++]_121/122/123/188.Best Time to Buy and Sell Stock I/II/III/IV
- LeetCode 122: 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 122, Medium] 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 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
- leetcode122.[DP] Best Time to Buy and Sell Stock II
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
- LeetCode(122) Best Time to Buy and Sell Stock II
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
- LeetCode 122 Best Time to Buy and Sell Stock II (贪心)
- [LeetCode]题解(python):122-Best Time to Buy and Sell Stock II