Skip to content

Commit 0722dea

Browse files
authored
Update Best Time to Buy and Sell Stock.java
1 parent c9b88eb commit 0722dea

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

Easy/Best Time to Buy and Sell Stock.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
class Solution {
22
public int maxProfit(int[] prices) {
3+
int minPrice = prices[0];
34
int maxProfit = 0;
4-
int currMin = Integer.MAX_VALUE;
55
for (int price : prices) {
6-
if (price > currMin) {
7-
maxProfit = Math.max(maxProfit, price - currMin);
8-
}
9-
else {
10-
currMin = price;
6+
if (price < minPrice) {
7+
minPrice = price;
118
}
9+
maxProfit = Math.max(maxProfit, price - minPrice);
1210
}
1311
return maxProfit;
1412
}

0 commit comments

Comments
 (0)