Best Time to Buy and Sell Stock II [LeetCode]
2013-10-19 14:57
295 查看
Problem Description: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
Basic idea: from code below, it seems super easy. But intuitively, we may use one more variable "tmp_max_profit" to record every times we get the max profit.
Basic idea: from code below, it seems super easy. But intuitively, we may use one more variable "tmp_max_profit" to record every times we get the max profit.
class Solution { public: int maxProfit(vector<int> &prices) { // Note: The Solution object is instantiated only once and is reused by each test case. int max_profit = 0; for(int i = 0; i < prices.size(); i ++) { if(i + 1 >= prices.size()) break; if(prices[i + 1] > prices[i]) max_profit += prices[i + 1] - prices[i]; } return max_profit; } };
相关文章推荐
- 【leetcode】Best Time to Buy and Sell Stock II
- leetcode JAVA Best Time to Buy and Sell Stock II 3.43 难度系数3
- leetcode:Best Time to Buy and Sell Stock II
- [Leetcode] Best Time to Buy and Sell Stock I,II,III,IV
- [Leetcode 122, Medium] Best Time to Buy and Sell Stock II
- [Leetcode]Best Time to Buy and Sell Stock II
- LeetCode--Best Time to Buy and Sell Stock II
- [week 20][Leetcode]Best Time to Buy and Sell Stock II
- [LeetCode]Best Time to Buy and Sell Stock II
- leetcode之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
- LeetCode Best Time to Buy and Sell Stock II
- leetcode - Best Time to Buy and Sell Stock II
- leetcode-Best Time to Buy and Sell Stock II
- Leetcode: Best Time to Buy and Sell Stock I, II
- 【leetcode】Best Time to Buy and Sell Stock I && II && III
- [Leetcode] #121#122#123#188 Best Time to Buy and Sell Stock I & II & III & IV
- leetcode(121)(122)(123)(188) Best Time to Buy and Sell Stock I/II/III/IV JAVA代码
- leetcode — best-time-to-buy-and-sell-stock-ii