Created
August 26, 2023 12:47
-
-
Save vamsitallapudi/fbdccd6ae545c9e27f99ba0d16901a54 to your computer and use it in GitHub Desktop.
Best Time to Buy and Sell Stock - Leetcode blind 75 #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int maxProfit(int[] prices) { | |
int maxProfit = 0; | |
int minVal = Integer.MAX_VALUE; | |
for(int i = 0; i< prices.length;i++) { | |
if(prices[i] < minVal) { | |
minVal = prices[i]; | |
} | |
maxProfit = Math.max(maxProfit, prices[i] - minVal); | |
} | |
return maxProfit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment