Skip to content

Instantly share code, notes, and snippets.

@BryanJin
Last active September 13, 2018 02:45
Show Gist options
  • Save BryanJin/276cd820f931e0ed904368333adaa3ae to your computer and use it in GitHub Desktop.
Save BryanJin/276cd820f931e0ed904368333adaa3ae to your computer and use it in GitHub Desktop.
public int maxProfit(int[] prices) {
int maxProfit = 0;
int minPrice = Integer.MAX_VALUE;
for (int currPrice : prices) {
if(minPrice > currPrice)
minPrice = currPrice;
if (currPrice - minPrice > maxProfit)
maxProfit = currPrice - minPrice;
}
return maxProfit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment