-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetCode_121.java
More file actions
39 lines (33 loc) · 927 Bytes
/
Copy pathleetCode_121.java
File metadata and controls
39 lines (33 loc) · 927 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
// class Solution {
// public int maxProfit(int[] prices) {
// int res=0;
// for(int i=prices.length-1; i >0 ; i--){
// for(int j=i-1; j >= 0 ; j--){
// if(prices[i] > prices[j]){
// int diff = prices[i] - prices[j];
// if(diff > res){
// res= diff;
// }
// }
// }
// }
// return res;
// }
// }
class Solution {
public int maxProfit(int[] prices) {
int res=0;
if(prices.length==0){
return 0;
}
int min = prices[0];
for(int i = 0 ; i < prices.length ; i++){
if(prices[i] > min){
res = Math.max(res, prices[i]-min);
}else{
min = prices[i];
}
}
return res;
}
}