1423. Maximum Points You Can Obtain from Cards
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 55% Topics: Array, Sliding Window, Prefix Sum
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
class Solution(object):
def maxScore(self, cardPoints, k):
"""
:type cardPoints: List[int]
:type k: int
:rtype: int
"""
result, total, curr, left = float("inf"), 0, 0, 0
for right, point in enumerate(cardPoints):
total += point
curr += point
if right-left+1 > len(cardPoints)-k:
curr -= cardPoints[left]
left += 1
if right-left+1 == len(cardPoints)-k:
result = min(result, curr)
return total-result
Solution from kamyu104/LeetCode-Solutions · MIT