Posted on 2014-01-20 21:51
Uriel 阅读(148)
评论(0) 编辑 收藏 引用 所属分类:
LeetCode
跟Best Time to Buy and Sell Stock一样,而且可以进行N次买入/卖出操作,于是如果连续两天股票市值之差大于零就累加就行了~
1 class Solution {
2 public:
3 int maxProfit(vector<int> &prices) {
4 int profit[100010], mx = 0, n = prices.size();
5 for(int i = 0; i < n - 1; ++i) {
6 profit[i + 1] = prices[i + 1] - prices[i];
7 }
8 for(int i = 1; i < n; ++i) {
9 if(profit[i] > 0) mx += profit[i];
10 }
11 return mx;
12 }
13 };