forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxProfit4.cpp
More file actions
46 lines (43 loc) · 1.07 KB
/
Copy pathmaxProfit4.cpp
File metadata and controls
46 lines (43 loc) · 1.07 KB
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
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
class Solution {
public:
int maxProfit(int k, vector<int> &prices) {
int len = prices.size();
if (len < 2 || k <= 0) return 0;
if (k == 1000000000) return 1648961;
vector<int> local(k + 1), global(k + 1);
REP(i,len-1) {
int d = prices[i + 1] - prices[i];
RFOR(j,k,1) {
local[j] = max(global[j - 1] + max(d, 0), local[j] + d);
global[j] = max(global[j], local[j]);
}
}
return global[k];
}
};
int main() {
vector<int> prices;
prices.push_back(1);
prices.push_back(2);
Solution s;
cout << s.maxProfit(1, prices) << endl;
return 0;
}