Skip to content

Commit 68dd39b

Browse files
121. Best Time to Buy and Sell Stock (java)
1 parent 1c995e9 commit 68dd39b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
if (prices.length == 0) return 0;
4+
int m = prices[0] , l = 0;
5+
for (int i = 1; i < prices.length; i++) {
6+
int d = prices[i];
7+
if (d < m) {
8+
m = d;
9+
} else {
10+
int n = d - m;
11+
l = n > l ? n : l;
12+
}
13+
}
14+
return l;
15+
}
16+
}

0 commit comments

Comments
 (0)