The Indian Engineer

Problem 1014 Best Sightseeing Pair

Posted on 6 mins

Array Dynamic-Programming

Problem Statement

Link - Problem 1014

Question

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.

Example 1:

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

Example 2:

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

Constraints:

Solution

class Solution {
public:
    int maxScoreSightseeingPair(vector<int>& values) {
        int n = values.size(), maxScore = 0;
        int maxLeft = values[0] + 0;
        for(int j = 1; j < n; j++) {
            maxScore = max(maxScore, maxLeft + values[j] - j);
            maxLeft = max(maxLeft, values[j] + j);
        }
        return maxScore;
    }
};

Complexity Analysis

| Algorithm             | Time Complexity | Space Complexity |
| --------------------- | --------------- | ---------------- |
| Two-pointer traversal | O(n)            | O(1)             |

Explanation

Intial Thoughts

The problem involves maximizing the score of a pair of sightseeing spots. The score is determined by adding the values of the two spots and subtracting the distance between them. This suggests that we need to balance the values of the spots with their distances. We could start by examining the relationship between the values and the distances. The distance is determined by the indices of the spots, so we may need to use the indices to our advantage. We could try to use a brute force approach, but given the constraints, this may not be efficient. A more efficient approach might involve using a sliding window or iterating through the array to find the optimal pair. The time complexity is O(n) and space complexity is O(1), so we should aim for a solution that meets these constraints.

Intuitive Analysis

One way to intuitively solve this problem is to think of it as a trade-off between the values of the spots and their distances. We want to maximize the total value while minimizing the distance. We can start by keeping track of the maximum left value (maxLeft) and updating it as we iterate through the array. The current score is determined by adding maxLeft to the current value and subtracting the distance. By keeping track of the maximum score, we can efficiently find the optimal pair without having to examine every pair. This approach allows us to balance the values and distances while meeting the time and space complexity requirements. By breaking down the problem into smaller sub-problems and solving them iteratively, we can find an efficient solution.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Can you tell the best sightseeing spot in one pass (ie. as you iterate over the input?) What should we store or keep track of as we iterate to do this?