Hi @pranav
I did. Actually I’ve used the pseudo codes from “Chapter 4.1 - The Maximum Subarray Problem” of CLRS. Currently it passes all tests on EPI Judge.
Here it is and I’d be happy to hear your feedback:
public static double maxProfit(List<Double> array) {
return findMaxProfit(array, 0, array.size() - 1);
}
public static double findMaxProfit(List<Double> A, int low, int high) {
if (low == high) {
return 0;
} else {
int mid = (low + (high - low) / 2);
double left = findMaxProfit(A, low, mid);
double right = findMaxProfit(A, mid + 1, high);
double cross = findMaxCrossing(A, low, mid, high);
return Math.max(left, Math.max(right, cross));
}
}
public static double findMaxCrossing(List<Double> A, int low, int mid, int high) {
double minLeft = Double.MAX_VALUE;
for (int i = mid; i >= low; i--) {
minLeft = Math.min(A.get(i), minLeft);
}
double maxRight = Double.MIN_VALUE;
for (int j = mid + 1; j <= high; j++) {
maxRight = Math.max(A.get(j), maxRight);
}
return maxRight - minLeft;
}