package leetcode;

/**
 * https://leetcode.com/problems/best-sightseeing-pair/
 */
public class Problem1021 {
    public int maxScoreSightseeingPair(int[] A) {
        int answer = 0;
        for (int i = 0; i < A.length; i++) {
            for (int j = i + 1; j < A.length; j++) {
                int val = A[i] + A[j] + i - j;
                answer = Math.max(answer, val);
            }
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem1021 prob = new Problem1021();
        System.out.println(prob.maxScoreSightseeingPair(new int[]{8, 1, 5, 2, 6})); // 11
    }
}

@@ -157,32 +157,8 @@
 ) %7B%0A
-        int answer = 0;%0A
     
@@ -214,100 +214,124 @@
     
-for (int j = i + 1; j %3C A.length; j++) %7B%0A                int val = A%5Bi%5D + A%5Bj%5D + i - j;%0A    
+A%5Bi%5D += i;%0A        %7D%0A        int answer = 0;%0A        int maxSoFar = 0;%0A        for (int i = 0; i %3C A.length; i++) %7B%0A
     
@@ -368,35 +368,43 @@
 er, 
-val);%0A            %7D
+A%5Bi%5D + maxSoFar - (i * 2));
 %0A       
  %7D%0A 
@@ -403,221 +403,82 @@
     
- %7D%0A
      
-   return answer;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1021 prob = new Problem1021();%0A        System.out.println(prob.maxScoreSightseeingPair(new int%5B%5D%7B8, 1, 5, 2, 6%7D)); // 11
+maxSoFar = Math.max(maxSoFar, A%5Bi%5D);%0A        %7D%0A        return answer;
 %0A   

Solve problem 1021