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.
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
Input: values = [1,2]
Output: 2
The brute force solution simply involves calculating the score for each possible pair of indices (i, j) where i < j. The score for a pair is defined as A[i] + A[j] + i - j. We iterate through all possible pairs and compute this score, keeping track of the maximum score encountered.
This solution involves precomputing the maximum value of A[i] + i for each index, and then for each j, computing the maximum score using the precomputed values.
The most efficient approach is to calculate A[i] + i on the go while iterating. We keep track of the maximum A[i] + i encountered so far and update the result with each A[j] - j. This allows us to solve the problem in a single pass with constant space.