Last active
January 18, 2018 20:01
-
-
Save carlosmuvi/08f2e49f8c3f178f4c90b29ecabe557a to your computer and use it in GitHub Desktop.
This file contains 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 int maxProfit(int[] a) { | |
if (a.length <= 2) { | |
return 0; | |
} | |
int currentMin = a[0]; | |
int currentMaxProfit = 0; | |
for (int i = 1; i < a.length; i++) { | |
if (a[i] < currentMin) { | |
currentMin = a[i]; | |
} else { | |
int tempProfit = a[i] - currentMin; | |
if (tempProfit > currentMaxProfit) { | |
currentMaxProfit = tempProfit; | |
} | |
} | |
} | |
return currentMaxProfit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment