forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bestTimeToBuyAndSellStock.II.cpp
47 lines (43 loc) · 1.59 KB
/
bestTimeToBuyAndSellStock.II.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Source : https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
// Author : Hao Chen
// Date : 2014-06-18
/**********************************************************************************
*
* Say you have an array for which the ith element is the price of a given stock on day i.
*
* Design an algorithm to find the maximum profit. You may complete as many transactions
* as you like (ie, buy one and sell one share of the stock multiple times). However,
* you may not engage in multiple transactions at the same time (ie, you must sell the
* stock before you buy again).
*
**********************************************************************************/
class Solution {
public:
//
// find all of ranges: which start a valley with the nearest peak after
// add their delta together
//
int maxProfit(vector<int> &prices) {
int max=0, begin=0, end=0;
bool up=false, down=false;
for (int i=1; i<prices.size(); i++) {
if (prices[i] > prices[i-1] && up==false){ // goes up
begin = i-1;
up = true;
down = false;
}
if (prices[i] < prices[i-1] && down==false) { // goes down
end = i-1;
down = true;
up = false;
max += (prices[end] - prices[begin]);
}
}
// edge case
if (begin < prices.size() && up==true){
end = prices.size() - 1;
max += (prices[end] - prices[begin]);
}
return max;
}
};