Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 1423 · Prefix / Circular Window

Maximum Points You Can Obtain from Cards

LeetCode 1423 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Slide k picks from all-left to all-right.

நடுத்தரம்ArraySliding WindowPrefix Sum
LeetCode இல் திற ↗

நான் Maximum Points You Can Obtain from Cards (LeetCode 1423) ஐ இப்படித் தீர்த்தேன் — Prefix / Circular Window பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Taking k cards from the ends means taking i from the left and k − i from the right. Start with all k from the left, then move one card at a time from the left side to the right side, tracking the best total.

Approach

  1. 1

    Sum the first k cards.

  2. 2

    For i from k−1 down to 0: drop cardPoints[i] from the left sum, add the next card from the right end.

  3. 3

    Keep the maximum.

நான் எப்படித் தீர்த்தேன்

Two-sided windowPythonO(k) time · O(1) space
class Solution:
    def maxScore(self, cardPoints, k):
        total = 0
        lsum = 0
        rsum = 0
        n = len(cardPoints)
        for i in range(k):
            lsum += cardPoints[i]
            total = lsum
        rIndex = n - 1
        for i in range(k - 1, -1, -1):
            lsum = lsum - cardPoints[i]
            rsum += cardPoints[rIndex]
            rIndex -= 1
            total = max(total, lsum + rsum)
        return total
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Maximum Points You Can Obtain from Cards — அடிக்கடி கேட்கப்படும் கேள்விகள்

Maximum Points You Can Obtain from Cards பின்னாலுள்ள intuition என்ன?

Taking k cards from the ends means taking i from the left and k − i from the right. Start with all k from the left, then move one card at a time from the left side to the right side, tracking the best total.

Maximum Points You Can Obtain from Cards (LeetCode 1423) ஐ எப்படித் தீர்ப்பது?

1. Sum the first k cards. 2. For i from k−1 down to 0: drop cardPoints[i] from the left sum, add the next card from the right end. 3. Keep the maximum.

Maximum Points You Can Obtain from Cards இன் time மற்றும் space complexity என்ன?

Time: O(k). Space: O(1). (Accepted approach: Two-sided window.)

Maximum Points You Can Obtain from Cards இல் பொதுவான தவறுகள் என்ன?

Equivalent: minimise the middle window of length n − k.