Maximum Points You Can Obtain from Cards (Leetcode 1423)

Problem Link: https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/

class Solution:
    def maxScore(self, cardPoints: List[int], k: int) -> int:
        
        n=len(cardPoints)
        s=sum(cardPoints)    
        if(n==k):
            return s
        tempSum=0
        i=0
        j=0
        k=(n-k)
        ans=0
        while(j<n):
            tempSum=tempSum+cardPoints[j]
            
            if(j-i+1<k):
                j+=1
            
            elif(j-i+1==k):
                ans=max(ans,s-tempSum)
                
                tempSum=tempSum-cardPoints[i]
                
                i+=1
                j+=1
        
        return ans
            

Last updated