Best Sightseeing Pair

Medium Arrays & Hashing Kadane's Algorithm Original on LeetCode

Reported atNutanixZepto

Best Sightseeing Pair is a medium arrays & hashing problem solved with the kadane's algorithm pattern. The best approach, optimal (running best of values[i] + i), runs in O(n) time and O(1) space. Below are 2 approaches in Java, Python, C++ and JavaScript, from brute force (every pair) up.

Problem

You are given an array values where values[i] is how attractive the i-th sightseeing spot is. Spots i and j are j - i apart.

A pair of spots i < j has the score values[i] + values[j] + i - j: the sum of their attractiveness minus the distance between them. Return the highest score of any pair.

Examples

Example 1

Input
values = [4, 2, 9, 3, 6]
Output
13
Why
Spots 2 and 4 score 9 + 6 + 2 - 4 = 13. The adjacent pair 2 and 3 only reaches 9 + 3 + 2 - 3 = 11.

Example 2

Input
values = [5, 5]
Output
9
Why
The only pair: 5 + 5 + 0 - 1 = 9.

Constraints

  • 2 <= values.length <= 5 · 10^4
  • 1 <= values[i] <= 1000

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (2 examples, or your own). Submit also runs 11 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
Brute force (every pair)O(n²)O(1)
Optimal (running best of values[i] + i)O(n)O(1)

1Brute force (every pair)

TimeO(n²)n(n - 1) / 2 pairs.
SpaceO(1)Only the running maximum.

Try every pair i < j, compute values[i] + values[j] + i - j and keep the largest.

  1. For i from 0 to n - 2 and j from i + 1 to n - 1, compute the score.
  2. Track the maximum.
class Solution {
    public int maxScoreSightseeingPair(int[] values) {
        int best = Integer.MIN_VALUE;
        for (int i = 0; i < values.length; i++)
            for (int j = i + 1; j < values.length; j++)
                best = Math.max(best, values[i] + values[j] + i - j);
        return best;
    }
}

2Optimal (running best of values[i] + i)

TimeO(n)One pass over the array.
SpaceO(1)Two integers.

Rewrite the score as (values[i] + i) + (values[j] - j). Sweeping j from left to right, the best partner for j is the largest values[i] + i seen so far, so keep that single number and update the answer at each step.

  1. bestLeft = values[0] + 0, answer = -infinity.
  2. For j from 1 to n - 1: answer = max(answer, bestLeft + values[j] - j).
  3. Then bestLeft = max(bestLeft, values[j] + j).
  4. Return answer.
class Solution {
    public int maxScoreSightseeingPair(int[] values) {
        int bestLeft = values[0], best = Integer.MIN_VALUE;
        for (int j = 1; j < values.length; j++) {
            best = Math.max(best, bestLeft + values[j] - j);
            bestLeft = Math.max(bestLeft, values[j] + j);
        }
        return best;
    }
}

Edge cases to test

  • Exactly two spots
  • The best pair is adjacent, or far apart with large values
  • All values equal (then the best pair is adjacent)
  • Decreasing values

Hints

Hint 1

Split the score into a part that depends only on i and a part that depends only on j: (values[i] + i) + (values[j] - j).

Hint 2

For a fixed j, you want the largest values[i] + i among all i < j.

Hint 3

Keep that maximum as you sweep left to right.

FAQ

What is the best time complexity for Best Sightseeing Pair?

Optimal (running best of values[i] + i) runs in O(n) time and O(1) extra space. One pass over the array.

Which pattern does Best Sightseeing Pair use?

It is a arrays & hashing problem that uses the kadane's algorithm pattern. Other problems with the same pattern: Kadane's Algorithm - Simple, Find minimum subarray sum, Kadane's Algo - Circular Array - No Extra Space.

Is there a brute force solution for Best Sightseeing Pair?

Yes. Brute force (every pair) takes O(n²) time and O(1) space. Try every pair i < j, compute values[i] + values[j] + i - j and keep the largest.

Which edge cases should I test for Best Sightseeing Pair?

Exactly two spots; The best pair is adjacent, or far apart with large values; All values equal (then the best pair is adjacent); Decreasing values.