Leetcode # 1423 (Python): Maximum Points You Can Obtain from Cards

정욱·2021년 4월 25일
0

Leetcode

목록 보기
26/38

Maximum Points You Can Obtain from Cards

  • Difficulty: Medium
  • Type: Dynamic Programming
  • link

Problem

Solution

  • Key observation: when selecting i cards from the front, k-i cards are selected from the back
  • Make two lists (front,back) that saves the sum of selecting i cards from both front and back
  • Run every combination and find the maximum sum
  • Time Complexity: O(k)
  • Tabulation (Bottom-Up) Solution
class Solution:
    def maxScore(self, cardPoints: List[int], k: int) -> int:
        max_sum = 0
        front = [0]
        back = [0]
        for i in range(k):
            front.append(front[-1]+cardPoints[i])
            back.append(back[-1]+cardPoints[-(i+1)])
        for i in range(k+1):
            max_sum = max(max_sum,front[i]+back[k-i])
        return max_sum

1개의 댓글

comment-user-thumbnail
2022년 1월 21일

The most important thing is to have a reliable bank card to avoid being cheated and paying hidden costs. That is why it is critical to call the https://total-visa-card.pissedconsumer.com/customer-service.html and, of course, the netspend customer service company to find out what cards are accessible, for what, and so on.

답글 달기