Best Sightseeing Pair

You are given an integer array values where values[i] is the score of the i-th sightseeing spot. Two spots i < j are a distance j - i apart. The score of the pair is values[i] + values[j] + i - j: the sum of their scores minus the distance between them. Return the maximum score over all pairs.

30:00

Best Sightseeing Pair
medium
Free written solution
Topics
Companies

You are given an integer array values where values[i] is the score of the i-th sightseeing spot. Two spots i < j are a distance j - i apart. The score of the pair is values[i] + values[j] + i - j: the sum of their scores minus the distance between them. Return the maximum score over all pairs.

Example 1:
Input: {"values":[8,1,5,2,6]}
Output: 11
Constraints:
  • 2≤values.length≤5×1042 \leq \text{values.length} \leq 5 \times 10^4

  • 1≤values[i]≤10001 \leq \text{values}[i] \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"values":[8,1,5,2,6]}

Score of a pair = (values[i] + i) + (values[j] - j). Spot 0 is the first left end: it offers values[0] + 0 = 8.

spots along the road, bar height = value; the left end's value fades by 1 per step of distance

8

0

1

1

5

2

2

3

6

4

best left end i

right end j

values[i] - distance

best pair so far

best_i = values[i] + i

8

values[j] - j

-

candidate

-

best

0

Variables
VariableValue
best0
best_i8
DepthFunction Call
Stack empty
0/9