Skip to content

Instantly share code, notes, and snippets.

@mounicasrinivas
Created January 2, 2024 12:13
Show Gist options
  • Save mounicasrinivas/88663f5e2233b7efe614be2a3aa6d25b to your computer and use it in GitHub Desktop.
Save mounicasrinivas/88663f5e2233b7efe614be2a3aa6d25b to your computer and use it in GitHub Desktop.
Best Time To Buy And Sell
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