Skip to content

Commit 08c8109

Browse files
authored
Merge pull request neetcode-gh#2032 from PorasS/java-solutions
Create 0122-best-time-to-buy-and-sell-stock-II.java
2 parents d553554 + 7435e7e commit 08c8109

File tree

1 file changed

+15
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)