-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path121.cpp
More file actions
40 lines (33 loc) · 706 Bytes
/
Copy path121.cpp
File metadata and controls
40 lines (33 loc) · 706 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
40
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n=prices.size();
if(n==0)
{
return 0;
}
int A[n];
A[n-1]=prices[n-1];
for(int i=n-1;i>0;i--)
{
int p=A[i];
if(prices[i-1]>=p)
{
A[i-1]=prices[i-1];
}
else
{
A[i-1]=p;
}
}
int max=0;
for(int i=0;i<n;i++)
{
if((A[i]-prices[i])>=max)
{
max=A[i]-prices[i];
}
}
return max;
}
};