Posted on 2014-01-20 21:51
Uriel 閱讀(157)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
LeetCode
跟Best Time to Buy and Sell Stock一樣,而且可以進(jìn)行N次買入/賣出操作,于是如果連續(xù)兩天股票市值之差大于零就累加就行了~
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 };