-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxProfit.java
More file actions
35 lines (35 loc) · 769 Bytes
/
maxProfit.java
File metadata and controls
35 lines (35 loc) · 769 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
public class Solution {
public int maxProfit(int[] prices) {
/* int len=prices.length;
int pronow=0;
for(int i=0;i<len;i++)
{
int db=prices[i];
for(int j=i;j<len;j++)
{
if(prices[j]<db)
continue;
else
{
pronow=Math.max(prices[j]-db,pronow);
}
}
}
return pronow;
}
*/
if (prices.length == 0) {
return 0 ;
}
int max = 0 ;
int sofarMin = prices[0] ;
for (int i = 0 ; i < prices.length ; ++i) {
if (prices[i] > sofarMin) {
max = Math.max(max, prices[i] - sofarMin) ;
} else{
sofarMin = prices[i];
}
}
return max ;
}
}