Skip to content

Instantly share code, notes, and snippets.

@tiagopereira17
Created July 11, 2016 14:45
Show Gist options
  • Save tiagopereira17/42603da46b202d96d028c9ebf556f184 to your computer and use it in GitHub Desktop.
Save tiagopereira17/42603da46b202d96d028c9ebf556f184 to your computer and use it in GitHub Desktop.
Given a log of stock prices compute the maximum possible earning
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