Best Sightseeing Pair
Best Sightseeing Pair is a medium arrays & hashing problem solved with the kadane's algorithm pattern.
The best approach, optimal (running best of values[i] + i), runs in O(n) time and O(1) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from brute force (every pair) up.
Problem
You are given an array values where values[i] is how attractive the i-th sightseeing spot is. Spots i and j are j - i apart.
A pair of spots i < j has the score values[i] + values[j] + i - j: the sum of their attractiveness minus the distance between them. Return the highest score of any pair.
Examples
Example 1
- Input
values = [4, 2, 9, 3, 6]- Output
13- Why
- Spots 2 and 4 score 9 + 6 + 2 - 4 = 13. The adjacent pair 2 and 3 only reaches 9 + 3 + 2 - 3 = 11.
Example 2
- Input
values = [5, 5]- Output
9- Why
- The only pair: 5 + 5 + 0 - 1 = 9.
Constraints
2 <= values.length <= 5 · 10^41 <= values[i] <= 1000
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 11 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Brute force (every pair) | O(n²) | O(1) |
| Optimal (running best of values[i] + i) | O(n) | O(1) |
1Brute force (every pair)
O(n²)n(n - 1) / 2 pairs.O(1)Only the running maximum.Try every pair i < j, compute values[i] + values[j] + i - j and keep the largest.
- For i from 0 to n - 2 and j from i + 1 to n - 1, compute the score.
- Track the maximum.
class Solution {
public int maxScoreSightseeingPair(int[] values) {
int best = Integer.MIN_VALUE;
for (int i = 0; i < values.length; i++)
for (int j = i + 1; j < values.length; j++)
best = Math.max(best, values[i] + values[j] + i - j);
return best;
}
}class Solution:
def maxScoreSightseeingPair(self, values: List[int]) -> int:
n = len(values)
best = -inf
for i in range(n):
for j in range(i + 1, n):
best = max(best, values[i] + values[j] + i - j)
return bestclass Solution {
public:
int maxScoreSightseeingPair(vector<int>& values) {
int n = values.size(), best = INT_MIN;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
best = max(best, values[i] + values[j] + i - j);
return best;
}
};function maxScoreSightseeingPair(values) {
const n = values.length;
let best = -Infinity;
for (let i = 0; i < n; i++)
for (let j = i + 1; j < n; j++)
best = Math.max(best, values[i] + values[j] + i - j);
return best;
}2Optimal (running best of values[i] + i)
O(n)One pass over the array.O(1)Two integers.Rewrite the score as (values[i] + i) + (values[j] - j). Sweeping j from left to right, the best partner for j is the largest values[i] + i seen so far, so keep that single number and update the answer at each step.
- bestLeft = values[0] + 0, answer = -infinity.
- For j from 1 to n - 1: answer = max(answer, bestLeft + values[j] - j).
- Then bestLeft = max(bestLeft, values[j] + j).
- Return answer.
class Solution {
public int maxScoreSightseeingPair(int[] values) {
int bestLeft = values[0], best = Integer.MIN_VALUE;
for (int j = 1; j < values.length; j++) {
best = Math.max(best, bestLeft + values[j] - j);
bestLeft = Math.max(bestLeft, values[j] + j);
}
return best;
}
}class Solution:
def maxScoreSightseeingPair(self, values: List[int]) -> int:
best_left = values[0]
best = -inf
for j in range(1, len(values)):
best = max(best, best_left + values[j] - j)
best_left = max(best_left, values[j] + j)
return bestclass Solution {
public:
int maxScoreSightseeingPair(vector<int>& values) {
int bestLeft = values[0], best = INT_MIN;
for (int j = 1; j < (int)values.size(); j++) {
best = max(best, bestLeft + values[j] - j);
bestLeft = max(bestLeft, values[j] + j);
}
return best;
}
};function maxScoreSightseeingPair(values) {
let bestLeft = values[0], best = -Infinity;
for (let j = 1; j < values.length; j++) {
best = Math.max(best, bestLeft + values[j] - j);
bestLeft = Math.max(bestLeft, values[j] + j);
}
return best;
}Edge cases to test
- Exactly two spots
- The best pair is adjacent, or far apart with large values
- All values equal (then the best pair is adjacent)
- Decreasing values
Hints
Hint 1
Split the score into a part that depends only on i and a part that depends only on j: (values[i] + i) + (values[j] - j).
Hint 2
For a fixed j, you want the largest values[i] + i among all i < j.
Hint 3
Keep that maximum as you sweep left to right.
FAQ
What is the best time complexity for Best Sightseeing Pair?
Optimal (running best of values[i] + i) runs in O(n) time and O(1) extra space. One pass over the array.
Which pattern does Best Sightseeing Pair use?
It is a arrays & hashing problem that uses the kadane's algorithm pattern. Other problems with the same pattern: Kadane's Algorithm - Simple, Find minimum subarray sum, Kadane's Algo - Circular Array - No Extra Space.
Is there a brute force solution for Best Sightseeing Pair?
Yes. Brute force (every pair) takes O(n²) time and O(1) space. Try every pair i < j, compute values[i] + values[j] + i - j and keep the largest.
Which edge cases should I test for Best Sightseeing Pair?
Exactly two spots; The best pair is adjacent, or far apart with large values; All values equal (then the best pair is adjacent); Decreasing values.