2
public int maxScoreSightseeingPair(int[] A) {3
// A[i] + A[j] + i - j = (A[i]+i) + (A[j]-j)4
// think about dividing them into two parts5
// get the max of left part and right part, then add them6
// to get the maximum score8
// max(left) + max(right)9
// maximum score = max(A[i]+i) + max((A[j]-j))13
A [8, 1, 5, 2, 6], max = 018
=> max = max(max=0, left + right=8) = 819
Before moving to i=2, we need to update left part by20
comparing current left and right21
so ----> left = max(left=8, A[1]+1=2) = 825
=> max = max(max=8, left + right=11) = 1126
so ----> left = max(left=8, A[2]+2=7) = 830
=> max = max(max=11, left + right=9) = 1131
so ----> left = max(left=8, A[3]+3=5) = 835
=> max = max(max=11, left + right=10) = 1136
so ----> left = max(left=8, A[4]+4=10) = 845
for (int i = 1; i < N; i++) {47
max = Math.max(max, left + right);48
// before we move on, we need to update the state of left part49
// now our current right part will become left part in next round50
left = Math.max(left, A[i] + i);