Best Sightseeing Pair

Medium Topics Company Tags

Choose two different positions i < j in values. Their score is values[i] + values[j] - (j - i): the two values minus their distance. Return the highest score among all pairs.

Example 1:

Input: values = [5,1,5]

Output: 8

Explanation: Choose indices 0 and 2. Their score is 5 + 5 + 0 - 2 = 8.

Example 2:

Input: values = [1,1]

Output: 1

Explanation: The only pair is (0, 1), with score 1 + 1 + 0 - 1 = 1.

Example 3:

Input: values = [2,9,1,2]

Output: 10

Explanation: Choose indices 0 and 1. Their score is 2 + 9 + 0 - 1 = 10, the greatest score among the available pairs.

Constraints:

  • 2 <= values.length <= 50000
  • 1 <= values[i] <= 1000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.