1014. Best sightseeing combination
Title Source: leetcode https://leetcode-cn.com/problems/best-sightseeing-pair
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.
Input: [8,1,5,2,6] Output: 11 Explanation: I = 0, j = 2, a [i] + a [J] + I - J = 8 + 5 + 0 - 2 = 11
- 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 parts
A[i] + iand
A[j] - j。
When traversing an array, for
A[j]For example, this value is fixed,
jIt’s also fixed. therefore
A[j]-jThis value is also fixed.
A[i] + i + A[j] - j, (i<j), for
A[j]In other words, to maximize the result, that is to seek the best solution
A[i]+iWhen you get the maximum.
In the end, at all levels
A[j]Returns the result that gets the largest in.
The specific code implementation is as follows.
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 max_value = A + 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, where I = J = 1 max_value = max(max_value, A[j] + j) return ans
- 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 here
A[j]It can be fixed here
A[j]-jThen the formula to find the maximum value is to find the maximum value
- In the end, at all levels
A[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.