https://leetcode.com/problems/best-sightseeing-pair/
X. Scan from right to left
https://leetcode.com/problems/best-sightseeing-pair/discuss/261067/Detailed-Explanation-using-DP-O(n)-Time-or-O(1)-Space
X.
https://leetcode.com/problems/best-sightseeing-pair/discuss/260909/JavaPython-Descriptive-solution.O(N)-Time-or-O(1)-Space.-Very-similar-to-Kadence-Algo!
We iterate each value
update
https://leetcode.com/problems/best-sightseeing-pair/discuss/261411/Java-O(N)-Easy-to-Understand-Clean-Code-beats-100
Given an array
A
of positive integers, A[i]
represents the value of the i
-th sightseeing spot, and two sightseeing spots i
and j
have distance j - i
between them.
The score of a pair (
i < j
) of sightseeing spots is (A[i] + A[j] + i - j)
: the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
X. Scan from right to left
https://leetcode.com/problems/best-sightseeing-pair/discuss/261067/Detailed-Explanation-using-DP-O(n)-Time-or-O(1)-Space
int Solution :: maxScoreSightseeingPair(vector<int>& a)
{
int n = a.size();
int maxOverallGain = INT_MIN;
int maxEndRight = a[n-1] - (n-1);
for(int i=n-2; i>=0; i--)
{
maxEndRight = max(maxEndRight, a[i+1] - (i+1));
maxOverallGain = max(maxOverallGain, a[i] + i + maxEndRight);
}
return maxOverallGain;
}
X.
https://leetcode.com/problems/best-sightseeing-pair/discuss/260909/JavaPython-Descriptive-solution.O(N)-Time-or-O(1)-Space.-Very-similar-to-Kadence-Algo!
The goal is to keep track of:
- Maximum So far and add it to the cur_cell and maintain maximum result
- Here, max_so_far contains : A[i] + i
Original Given Formula : A[i] + A[j] + i - j
- Break in two parts : A[i] + i and A[j] -j
- Keep MAX_VALUE of first part among the elements seen so far
- Add the current element to max_so_far and check the result is changing or not
- Also, keep updating the max_so_far at each step
public int maxScoreSightseeingPair(int[] a) {
int max_so_far = a[0];
int result = 0;
for(int i=1;i<a.length;i++){
result = Math.max(result, max_so_far + a[i] - i);
max_so_far = Math.max(max_so_far, a[i] + i);
}
return result;
}
https://leetcode.com/problems/best-sightseeing-pair/discuss/260850/JavaC%2B%2BPython-One-Pass
Count the current best score in all previous sightseeing spot.
Note that, as we go further, the score of previous spot decrement.
Note that, as we go further, the score of previous spot decrement.
Explanation
cur
will record the best score that we have met.We iterate each value
a
in the array A
,update
res
by max(res, cur + a)
Also we can update
Note that when we move forward,
all sightseeing spot we have seen will be 1 distance further.
cur
by max(cur, a)
.Note that when we move forward,
all sightseeing spot we have seen will be 1 distance further.
So for the next sightseeing spot
cur = Math.max(cur, a) **- 1**
It's kinds of like, "A near neighbor is better than a distant cousin."
https://leetcode.com/problems/best-sightseeing-pair/discuss/260884/C%2B%2B-O(n)-best-time-to-buy-and-sell-stock
It's similar to Best Time to Buy and Sell Stock, but instead of min price, we track max value, and our max value decays every step due to the distance penalty.
Solution
- Track the maximum value of
A[i]
asmax_i
. - Every turn, decrement
max_i
to account forj - i
. - Track and return the maximum score.
public int maxScoreSightseeingPair(int[] A) {
int max = A[0];
int res = Integer.MIN_VALUE;
for (int i = 1; i < A.length; i++) {
max = Math.max(max, A[i - 1] + i - 1);
res = Math.max(res, max + A[i] - i);
}
return res;
}
https://leetcode.com/problems/best-sightseeing-pair/discuss/261411/Java-O(N)-Easy-to-Understand-Clean-Code-beats-100