-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path121.java
More file actions
36 lines (35 loc) · 1.07 KB
/
121.java
File metadata and controls
36 lines (35 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
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public int maxProfit(int[] prices) {
int res = 0;
if (prices.length < 2) return res;
int min = prices[0];
for (int p: prices) {
if (p > min) res = Math.max(res, p - min);
if (p < min) min = p;
}
return res;
}
}
__________________________________________________________________________________________________
sample 34412 kb submission
class Solution {
public int maxProfit(int[] prices) {
if(prices==null || prices.length<=1){
return 0;
}
int min = prices[0];
int max = 0;
for(int i=1; i<prices.length; i++){
if(min>prices[i]){
min=prices[i];
}
if(prices[i]-min>max){
max=prices[i]-min;
}
}
return max;
}
}
__________________________________________________________________________________________________