Leetcode 1014. Best sightseeing combination | Python

Time:2021-3-14

1014. Best sightseeing combination


Title Source: leetcode https://leetcode-cn.com/problems/best-sightseeing-pair

subject


Given a positive integer array a, a [i] represents the score of the ith scenic spot, and the distance between two scenic spots I and j is J – I.

The score of a tourism combination composed of a pair of scenic spots (I < J) is (a [i] + a [J] + I – J): the sum of the scores of scenic spots minus the distance between them.

Return to the highest score a pair of tourist attractions can get.

Example:

Input: [8,1,5,2,6]
Output: 11
Explanation: I = 0, j = 2, a [i] + a [J] + I - J = 8 + 5 + 0 - 2 = 11

Tips:

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

Thinking of solving problems


After examining the question, we found that the answer to the question has been reflected in the question. [the score of a tourism combination composed of a pair of scenic spots (I < J) is (a [i] + a [J] + I – J): the sum of the scores of scenic spots minus the distance between them]. Here, we can find that applying the above formula, we can traverse and compare to get the maximum.

When we use brute force solution, the code is as follows:

def maxScoreSightseeingPair(self, A: List[int]) -> int:
    length = len(A)
    max_score = 0
    for i in range(length):
        for j in range(i+1, length):
            score = A[i] + A[j] + i - j
            if score > max_score:
                max_score = score
    return max_score

However, all the use cases (execution result: timeout) cannot be passed here.

Although the execution will time out, the direction of this idea is not wrong. Let’s take a closer look at the formula given in the title

A[i] + A[j] + i - j, (i<j)

We turn it into:

A[i] + i + A[j] - j, (i<j)

It is easy to see that the formula can be divided into two partsA[i] + iandA[j] - j

When traversing an array, forA[j]For example, this value is fixed,A[j]Corresponding indexjIt’s also fixed. thereforeA[j]-jThis value is also fixed.

Now askA[i] + i + A[j] - j, (i<j), forA[j]In other words, to maximize the result, that is to seek the best solutionA[i]+iWhen you get the maximum.

In the end, at all levelsA[j]Returns the result that gets the largest in.

The specific code implementation is as follows.

code implementation


class Solution:
    def maxScoreSightseeingPair(self, A: List[int]) -> int:
        length = len(A)
        #Change the formula to a [i] + I + a [J] - J, (I < J)
        #Split into a [i] + I, a [J] - J
        #A [i] + I is initialized to a [0] + 0
        max_value = A[0] + 0
        ans = 0
        for j in range(1, length):
            #Maintenance update maximum
            #Note here that I < J
            ans = max(ans, max_value + A[j] - j)
            #The value of a [i] + I is maintained and updated here, which is equivalent to comparing with a [I-1] + (i-1)
            # max_ Value is initialized to a [0] + 0, where I = J = 1
            max_value = max(max_value, A[j] + j)

        return ans

Implementation results


Leetcode 1014. Best sightseeing combination | Python

summary


  • According to the meaning of the question, you can use violent solution to try to solve the problem first. Although it will time out here, it can be determined that the direction is right.
  • The formula given in the title is converted to the following:A[i] + i + A[j] - j, (i<j)We need to pay attention herei<j
  • aboutA[j]It can be fixed hereA[j]-jThen the formula to find the maximum value is to find the maximum valueA[i]+imaximum;
  • In the end, at all levelsA[j]Select the largest result to return.

The article is original. If you think it is well written, you are welcome to pay attention to it. The official account of WeChat is updated in parallel.

Recommended Today

Rust programming video tutorial (Advanced) – 024_ 3 syntax of all modes 3

Video address Headline address:https://www.ixigua.com/i677586170644791348…Station B address:https://www.bilibili.com/video/av81202308/ Source address GitHub address:https://github.com/anonymousGiga/learn_rus… Explanation content 1. Ignore values in mode(1) Use_ Ignore entire valueexample: fn foo(_: i32, y: i32) { println!(“This code only uses the y parameter: {}”, y); } fn main() { foo(3, 4); } Note: placeholders are used for parameters in the function, mainly when implementing […]