Zepruv › Coding problems

Best Sightseeing Pair

Medium · Array, Dynamic Programming

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

i = 0, j = 2, values[i] + values[j] + i - j = 8 + 5 + 0 - 2 = 11

Example 2

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

Constraints

Starter code (python)

class Solution(object):
    def maxScoreSightseeingPair(self, values):
        """
        :type values: List[int]
        :rtype: int
        """
        

Related problems

Previous: Partition Array Into Three Parts With Equal Sum · Next: Binary String With Substrings Representing 1 To N · All problems

Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.