-
Notifications
You must be signed in to change notification settings - Fork 0
/
BestTimeToBuyAndSellStock4.cpp
40 lines (39 loc) · 1.04 KB
/
BestTimeToBuyAndSellStock4.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
/**
* @file BestTimeToBuyAndSellStock4.cpp
* @brief
* @author nhzc [email protected]
* @version 1.0.0
* @date 2015-04-19
*/
class Solution {
public:
//http://www.cnblogs.com/grandyang/p/4295761.html
int totalProfit(vector<int> &prices){
int profit = 0;
for (int i = 1; i < prices.size(); i++){
if(prices[i] - prices[i - 1] > 0){
profit += (prices[i] - prices[i - 1]);
}
}
return profit;
}
int maxProfit(int k, vector<int> &prices) {
int result = 0;
if (prices.size() == 0){
return result;
}
if (k >= prices.size()){
return totalProfit(prices);
}
int g[k + 1] = {0};
int l[k + 1] = {0};
for (int i = 0; i < prices.size() - 1; i++){
int diff = prices[i + 1] - prices[i];
for (int j = k; j >= 1; j --){
l[j] = max(g[j - 1] + max(diff, 0), l[j] + diff);
g[j] = max(g[j], l[j]);
}
}
return g[k];
}
};