-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathBest Sightseeing Pair.java
56 lines (52 loc) · 1.93 KB
/
Best Sightseeing Pair.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Runtime: 4 ms (Top 77.63%) | Memory: 50.10 MB (Top 66.67%)
class Solution {
public int maxScoreSightseeingPair(int[] A) {
// A[i] + A[j] + i - j = (A[i]+i) + (A[j]-j)
// think about dividing them into two parts
// get the max of left part and right part, then add them
// to get the maximum score
//
// max(left) + max(right)
// maximum score = max(A[i]+i) + max((A[j]-j))
/* example:
i 0 1 2 3 4
A [8, 1, 5, 2, 6], max = 0
i=1,
left = A[0]+0 = 8
right = A[1]-1 = 0
=> max = max(max=0, left + right=8) = 8
Before moving to i=2, we need to update left part by
comparing current left and right
so ----> left = max(left=8, A[1]+1=2) = 8
i=2,
left = 8
right = A[2]-2 = 3
=> max = max(max=8, left + right=11) = 11
so ----> left = max(left=8, A[2]+2=7) = 8
i=3,
left = 8
right = A[3]-3 = 1
=> max = max(max=11, left + right=9) = 11
so ----> left = max(left=8, A[3]+3=5) = 8
i=4,
left = 8
right = A[4]-4 = 2
=> max = max(max=11, left + right=10) = 11
so ----> left = max(left=8, A[4]+4=10) = 8
end loop
max = 11
*/
int N = A.length;
int left = A[0]+0;
int right = 0;
int max = 0;
for (int i=1; i<N; i++){
right = A[i]-i;
max = Math.max(max, left+right);
// before we move on, we need to update the state of left part
// now our current right part will become left part in next round
left = Math.max(left, A[i]+i);
}
return max;
}
}