Here is another codility problem solution from the codility lessons (MaxProfit -Given a log of stock prices compute the maximum possible earning.
) due to the copy rights I can't copy the content of the problem here so to view the problem description click here.
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int max = 0;
int min = Integer.MAX_VALUE;
int profit = Integer.MIN_VALUE;
if (A.length < 2) return 0;
for (int i = 0; i < A.length; i++) {
min = Math.min(min, A[i]);
int def = A[i] - min;
profit = Math.max(profit, def);
}
return Math.abs(profit);
}
}
No comments:
Post a Comment