Skip to main content

1423 - Maximum Points You Can Obtain from Cards

Details

KeyValue
Linkhttps://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/
LanguagePython 3
Runtime544 ms, faster than 59.98% of Python3 online submissions for Maximum Points You Can Obtain from Cards
Memory Usage27 MB, less than 87.94% of Python3 online submissions for Maximum Points You Can Obtain from Cards
DatastructuresList[int]
AlgorithmsSliding Window
ComplexityTime: O(k) Memory: O(1)

Procedure

  1. ...

Code

class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
number_of_cards = len(cardPoints)
sum_of_all_cards = sum(cardPoints)
sliding_window_size = number_of_cards - k
sliding_window_sum = sum(cardPoints[:sliding_window_size])
minimum_sum = sliding_window_sum

for i in range(sliding_window_size, number_of_cards):
sliding_window_sum += cardPoints[i]
sliding_window_sum -= cardPoints[i - sliding_window_size]
minimum_sum = min(minimum_sum, sliding_window_sum)

return sum_of_all_cards - minimum_sum