Likou brushing notes: 1423. The maximum number of points that can be obtained (slide the window to take the minimum value, ingeniously solve the problem, complete the code and comments of the problem solution)

topic:

1423、Maximum points available

Several cards are arranged in a row, and each card has a corresponding point. The number of points is given by the integer array cardPoints.

For each action, you can take a card from the beginning or the end of the line, and in the end you must take exactly k cards.

Your points are the sum of the points of all the cards in your hand.

Give you an integer array cardPoints and an integer k, please return the maximum number of points you can get.

Example 1:

Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: In the first action, no matter which card you take, your point is always 1. However, taking the rightmost card first will maximize your available points. The optimal strategy is to take the three cards on the right, and the final point is 1 + 6 + 5 = 12.

Example 2:

Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: No matter which two cards you pick up, the number of points you can get is always 4.

Example 3:

Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You must pick up all the cards, and the points you can get are the sum of the points of all the cards.

Example 4:

Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot get the card in the middle, so the maximum number of points you can get is 1.

Example 5:

Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202

prompt:

1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length

Problem solution ideas:

Ideas

Remember that the length of the array cardPoints is n. Since only k cards can be taken from the beginning and the end, the last remaining must be consecutive nk cards.

We can find the maximum value of the sum of card points by finding the minimum value of the sum of the remaining card points.

algorithm

Since the remaining cards are continuous, a sliding window with a fixed length of nk is used to traverse the array cardPoints to find the minimum value of the sliding window, and then subtract the minimum value from the sum of the points of all cards to get the take away The maximum value of the sum of card points.

Problem solution python code:

class Solution:
    def maxScore(self, cardPoints: List[int], k: int) -> int:
        n = len(cardPoints)
        # 滑动窗口大小为 n-k
        windowSize = n - k
        # 选前 n-k 个作为初始值
        s = sum(cardPoints[:windowSize])
        minSum = s
        for i in range(windowSize, n):
            # 滑动窗口每向右移动一格,增加从右侧进入窗口的元素值,并减少从左侧离开窗口的元素值
            s += cardPoints[i] - cardPoints[i - windowSize]
            minSum = min(minSum, s)
        return sum(cardPoints) - minSum

Author: LeetCode-Solution
Links: https://leetcode-cn.com/problems/maximum-points-you-can-obtain-from-cards/solution/ke-huo-de-de-zui-da-dian-shu -by-leetcode-7je9/
Source: LeetCode https://leetcode-cn.com/problems/maximum-points-you-can-obtain-from-cards/

Guess you like

Origin blog.csdn.net/weixin_44414948/article/details/113727700