Skip to content

Commit 6885833

Browse files
committed
[WEEK 05] best time to buy and sell stock
1 parent 9f896bb commit 6885833

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
int result = 0;
4+
// prices = [7,1,5,3,6,4]
5+
int lt = 0;
6+
for ( int i = 1 ; i < prices.length; i ++){
7+
if ( prices[i] < prices[lt] ){
8+
lt = i;
9+
}
10+
else{
11+
result = Math.max(result, prices[i] - prices[lt]);
12+
}
13+
}
14+
15+
return result;
16+
}
17+
}

0 commit comments

Comments
 (0)