Skip to content

Commit

Permalink
121-Best-Time-to-Buy-and-Sell-Stock
Browse files Browse the repository at this point in the history
  • Loading branch information
vanhieu-it committed Aug 13, 2024
1 parent 6ab5347 commit dfe9d23
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public int maxProfit(int[] prices) {
int min = prices[0];
int maxProfit = 0;

for (int i = 1; i < prices.length; i++) {
if (prices[i] < min) {
min = prices[i];
}
else if (prices[i] > min) {
if(prices[i] - min > maxProfit){
maxProfit = prices[i] - min;
}
}
}
return maxProfit;
}
}

0 comments on commit dfe9d23

Please sign in to comment.