Skip to content

Instantly share code, notes, and snippets.

@vamsitallapudi
Created August 26, 2023 12:47
Show Gist options
  • Save vamsitallapudi/fbdccd6ae545c9e27f99ba0d16901a54 to your computer and use it in GitHub Desktop.
Save vamsitallapudi/fbdccd6ae545c9e27f99ba0d16901a54 to your computer and use it in GitHub Desktop.
Best Time to Buy and Sell Stock - Leetcode blind 75 #2
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