Problem Solving/LeetCode
Today's Challenge
fabichoi
2022. 6. 26. 23:45
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/
Maximum Points You Can Obtain from Cards - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
오랜만에 내 손으로 풀어본 문제.
5번 재제출만에 풂 ㅋㅋㅋㅋ
그냥 직관적으로 풀면 되는데.. index가 자꾸 헷갈려서 몇 번 틀린 듯 ㅠㅠ
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
a = copy.deepcopy(cardPoints)
b = copy.deepcopy(cardPoints)
l = len(cardPoints)
if l == k:
return sum(cardPoints)
for i in range(1, l):
a[i] += a[i - 1]
b[l - i - 1] += b[l - i]
res = max(a[k - 1], b[-k])
for i in range(0, k - 1):
res = max(res, a[i] + b[(l-(k-i-1))])
return res
반응형