Skip to content

Commit 67abb3e

Browse files
authored
Create BestTimeToBuyAndSellStock.java
1 parent 9b5f3e3 commit 67abb3e

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

easy/BestTimeToBuyAndSellStock.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//121. Best Time Buy and Sell Stock
2+
class Solution {
3+
public int maxProfit(int[] prices) {
4+
int sellOne = 0;
5+
int holdOne = Integer.MIN_VALUE;
6+
7+
for (final int price : prices) {
8+
sellOne = Math.max(sellOne, holdOne + price);
9+
holdOne = Math.max(holdOne, -price);
10+
}
11+
12+
return sellOne;
13+
}
14+
}

0 commit comments

Comments
 (0)