엔지니어 게시판
LeetCode 솔루션 분류

[6/26] 1423. Maximum Points You Can Obtain from Cards

컨텐츠 정보

본문

There are several cards arranged in a row, and each card has an associated number of points. The points are given in the integer array cardPoints.

In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.

Your score is the sum of the points of the cards you have taken.

Given the integer array cardPoints and the integer k, return the maximum score you can obtain.

 

Example 1:

Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.

Example 2:

Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.

Example 3:

Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.

 

Constraints:

  • 1 <= cardPoints.length <= 105
  • 1 <= cardPoints[i] <= 104
  • 1 <= k <= cardPoints.length

관련자료

댓글 1

학부유학생님의 댓글

  • 익명
  • 작성일
class Solution:
    def maxScore(self, cardPoints: List[int], k: int) -> int:
        
        total_sum = sum(cardPoints)
        sub_len = len(cardPoints) - k
        if not sub_len: return total_sum
        
        curr_sum = 0
        l = 0
        min_sum = float('inf')
        
        for r in range(len(cardPoints)):
            curr_sum += cardPoints[r]
            
            if r -l+1 == sub_len:
                min_sum = min(min_sum, curr_sum)
                curr_sum -= cardPoints[l]
                l += 1
                

        
        return total_sum - min_sum
            
            
전체 94 / 2 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 703 명
  • 오늘 방문자 4,011 명
  • 어제 방문자 7,431 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,542 명
알림 0