Best Time to Buy and Sell Stock II [LeetCode]
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.
1 class Solution { 2 public: 3 int maxProfit(vector<int> &prices) { 4 // Note: The Solution object is instantiated only once and is reused by each test case. 5 int max_profit = 0; 6 for(int i = 0; i < prices.size(); i ++) { 7 if(i + 1 >= prices.size()) 8 break; 9 10 if(prices[i + 1] > prices[i]) 11 max_profit += prices[i + 1] - prices[i]; 12 } 13 14 return max_profit; 15 } 16 };