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.