Created
July 11, 2016 14:45
-
-
Save tiagopereira17/42603da46b202d96d028c9ebf556f184 to your computer and use it in GitHub Desktop.
Given a log of stock prices compute the maximum possible earning
This file contains hidden or 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 class MaxProfit { | |
public int solution(int[] A) { | |
if(A == null || A.length < 2) { | |
return 0; | |
} | |
int maxProfit = 0; | |
int min = -1; | |
for(int i = 0; i < A.length; i++) { | |
if(min == -1 || min > A[i]) { | |
min = A[i]; | |
continue; | |
} | |
maxProfit = Math.max(maxProfit, A[i] - min); | |
} | |
return maxProfit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment