Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2032 from PorasS/java-solutions
Browse files Browse the repository at this point in the history
Create 0122-best-time-to-buy-and-sell-stock-II.java
  • Loading branch information
tahsintunan authored Jan 24, 2023
2 parents d553554 + 7435e7e commit 08c8109
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions java/0122-best-time-to-buy-and-sell-stock-II.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public int maxProfit(int[] prices) {
int oldStockPrice = prices[0];
int profit = 0;
for(int i = 1; i<prices.length; i++){
if(prices[i]<oldStockPrice){
oldStockPrice = prices[i];
}else{
profit+=prices[i]-oldStockPrice;
oldStockPrice = prices[i];
}
}
return profit;
}
}

0 comments on commit 08c8109

Please sign in to comment.