Created
January 2, 2024 12:13
-
-
Save mounicasrinivas/88663f5e2233b7efe614be2a3aa6d25b to your computer and use it in GitHub Desktop.
Best Time To Buy And Sell
This file contains hidden or 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
public class BestTimeToBuyAndSell { | |
public int maxProfit(int[] prices) { | |
int minValue = Integer.MAX_VALUE; | |
int maxProfit = 0; | |
for (int i = 0; i < prices.length; i++) { | |
if (prices[i] < minValue) { | |
minValue = prices[i]; | |
} | |
maxProfit = Math.max(maxProfit, prices[i] - minValue); | |
} | |
return maxProfit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment