forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxProfit3.cpp
More file actions
39 lines (32 loc) · 869 Bytes
/
maxProfit3.cpp
File metadata and controls
39 lines (32 loc) · 869 Bytes
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
#include <iostream>
#include <vector>
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(vector<int> &prices) {
int n = prices.size();
if (n <= 1) return 0;
vector<int> mm1(n, 0), mm2(n, 0);
int mi = prices[0];
FOR(i,1,n-1) {
mm1[i] = max(mm1[i - 1], prices[i] - mi);
mi = min(mi, prices[i]);
}
int mx = prices[n - 1];
RFOR(i,n-2,0) {
mm2[i] = max(mm2[i + 1], mx - prices[i]);
mx = max(mx, prices[i]);
}
int ret = mm1[n - 1];
FOR(i,0,n-2) {
ret = max(ret, mm1[i] + mm2[i + 1]);
}
return ret;
}
};
int main() {
return 0;
}