Skip to content

Instantly share code, notes, and snippets.

@sreeprasad
Created November 11, 2014 10:55
Show Gist options
  • Save sreeprasad/4641fa8621797c38f5d8 to your computer and use it in GitHub Desktop.
Save sreeprasad/4641fa8621797c38f5d8 to your computer and use it in GitHub Desktop.
Best Time to Buy and Sell Stock
public int maxProfit(int[] prices) {
int min = Integer.MAX_VALUE;
int maxProfit=0;
for(int i=0;i<prices.length;i++){
if(min>prices[i]){
min=prices[i];
}
if( (prices[i]-min)>maxProfit ){
maxProfit = prices[i]-min;
}
}
return maxProfit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment