-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path123.cpp
More file actions
35 lines (35 loc) · 884 Bytes
/
123.cpp
File metadata and controls
35 lines (35 loc) · 884 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
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.empty())
return 0;
int n = prices.size();
vector<int> first(n,0);
vector<int> second(n,0);
int mn = prices[0];
int mx = prices[n-1];
int p = 0;
for(int i=1;i<n; i++){
if(prices[i]>mn)
first[i] = prices[i]-mn;
else
mn = prices[i];
p = max(p,first[i]);
first[i] = p;
}
p = 0;
for(int i=n-2; i>=0; i--){
if(prices[i]<mx)
second[i] = mx-prices[i];
else
mx = prices[i];
p = max(p,second[i]);
second[i] = p;
}
int res= 0;
for(int i=0;i<n;i++){
res = max(res,first[i]+second[i]);
}
return res;
}
};