Best Sightseeing Pair
You can approach this problem by finding the maximum score of a pair of sightseeing spots. You need to maximize ( \text{values}[i] + \text{values}[j] + i - j ) for all ( i < j ).
Here’s the code to do that:
|
|
The idea is to iterate through the sightseeing spots and maintain a running maximum of the score as current_score
. By tracking the running maximum, you can update the maximum score for each pair efficiently, resulting in an (O(n)) time complexity.