Clean Java Sol NLOGN


#1
public int solve(int[] A) {
    int maxSum = Integer.MIN_VALUE;
    if (A.length < 3) return 0;
    int[] maxR = new int[A.length];
    int max = Integer.MIN_VALUE;
    for (int i = A.length - 1; i >= 0; i--) {
        if (A[i] > max) {
            maxR[i] = A[i];
            max = A[i];
        } else maxR[i] = max;
    }
    TreeSet<Integer> maxL = new TreeSet();
    maxL.add(A[0]);

    for (int i = 1; i < A.length - 1; i++) {
        Integer left = maxL.lower(A[i]);
        if (maxR[i + 1] > A[i] && left != null) {
            int sum = A[i] + left + maxR[i + 1];
            if (sum > maxSum) maxSum = sum;
        }
        maxL.add(A[i]);
    }

    return maxSum;
}