You are given an integer array values
where values[i] represents the value of the ith
sightseeing spot. Two sightseeing spots i
and j
have a distance j - i
between them.
The score of a pair (i < j
) of sightseeing spots is values[i] + values[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: values = [8,1,5,2,6] Output: 11 Explanation: i = 0, j = 2, values[i] + values[j] + i - j = 8 + 5 + 0 - 2 = 11
Example 2:
Input: values = [1,2] Output: 2
Constraints:
2 <= values.length <= 5 * 104
1 <= values[i] <= 1000
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the best sightseeing pair involves checking every single possible pair of locations. We want to find the pair that gives us the highest sightseeing score. This means trying every combination and comparing their scores.
Here's how the algorithm would work step-by-step:
def best_sightseeing_pair_brute_force(values):
highest_sightseeing_score = 0
# Iterate through each location as the first in the pair
for first_location_index in range(len(values)):
for second_location_index in range(len(values)):
# Ensure we are not comparing the same location to itself
if first_location_index != second_location_index:
sightseeing_score = values[first_location_index] + values[second_location_index] + first_location_index - second_location_index
# Keep track of the largest score
if sightseeing_score > highest_sightseeing_score:
highest_sightseeing_score = sightseeing_score
return highest_sightseeing_score
The core idea is to keep track of the best 'potential' starting point as we go. Instead of checking every possible pair, we efficiently update what the best first element of a pair could be, letting us maximize the score by pairing it with later elements.
Here's how the algorithm would work step-by-step:
def best_sightseeing_pair(values):
max_combined_score = 0
best_starting_score_so_far = values[0] + 0
for index in range(1, len(values)):
#Calculate score of current location paired with best previous location.
current_combined_score = best_starting_score_so_far + values[index] - index
max_combined_score = max(max_combined_score, current_combined_score)
# Check if the current location offers a better starting point.
best_starting_score_so_far = max(best_starting_score_so_far, values[index] + index)
return max_combined_score
Case | How to Handle |
---|---|
Empty or null input array | Return 0 immediately since no valid pair can exist. |
Array with only one element | Return 0 since a sightseeing pair requires at least two distinct elements. |
Array with all identical values | The algorithm should correctly handle identical values and find the best pair among them. |
Array with large indices causing integer overflow during calculation of score | Use long data type to prevent integer overflow when calculating the score (A[i] + A[j] + i - j). |
Array with negative values | The problem statement doesn't restrict the input to positive integers, so the algorithm should handle negative values correctly. |
Array with a very large number of elements (scaling considerations) | The provided greedy approach with O(n) time complexity is efficient and scales well with a large input array. |
Array with extreme boundary values for integers (min/max int) | Ensure calculations A[i] + A[j] + i - j does not cause overflow/underflow using long data types. |
Multiple pairs yield the same maximum score | The problem asks for the maximum score, and the algorithm will return one such score even if multiple pairs yield the same result, which is acceptable. |