您的位置:首页 > 其它

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.

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