Maximum Points You Can Obtain from Cards

Leetcode#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

Logic

  • Logic is similar to the one mentioned in Max Sum of K Window.
  • But we need to calculate sum of remaining numbers and delete the k items.

Hint 1

  • Let the sum of all points be total_pts.
  • You need to remove a sub-array from cardPoints with length n – k.

Hint 2

  • Keep a window of size n – k over the array.
  • The answer is MAX(answer, total_pts – sumOfCurrentWindow)

Golang solution

func maxScore(cardPoints []int, k int) int {
    totalSum := 0

    for i := range cardPoints {
        totalSum += cardPoints[i] 
    }

    if len(cardPoints) == k {
        return totalSum
    }

    windowSize := len(cardPoints) - k
    currentSum := 0
    for i:=0; i<windowSize;i++{
        currentSum += cardPoints[i] 
    }

    maxSum := totalSum-currentSum
    leftIndex := 0
    rightIndex := windowSize-1

    for ; rightIndex < len(cardPoints)-1; {
        currentSum = currentSum - cardPoints[leftIndex]
        leftIndex++
        rightIndex++
        currentSum = currentSum + cardPoints[rightIndex]
        maxSum = max(maxSum, totalSum-currentSum)
    }
    return maxSum
}

Output

cardPoints =[1,2,3,4,5,6,1]
k = 3
Output 12

Git diff compare to max-sum-of-k-window

func maxSumOfKWindow(arr []int, k int) int {
+ func maxScore(cardPoints []int, k int) int {
+ 
+     totalSum := 0
+ 
+     for i := range cardPoints {
+         totalSum += cardPoints[i] 
+     }
+ 
+     if len(cardPoints) == k {
+         return totalSum
+     }
+ 
// Diff-1
+     windowSize := len(cardPoints) - k
      currentSum := 0
-     for i := 0; i < k; i++ {
+     for i := 0; i < windowSize; i++ {
          currentSum += arr[i]
+         currentSum += cardPoints[i] 
      }

// Diff-2
-     maxSum := currentSum
+     maxSum := totalSum - currentSum

-     right := k - 1
+     rightIndex := windowSize - 1
-     for right < len(arr)-1 {
+     for ; rightIndex < len(cardPoints)-1; {
// Diff-3
-         maxSum = max(maxSum, currentSum)
+         maxSum = max(maxSum, totalSum - currentSum)
      }
      return maxSum
  }

Prefix Sum solution for circular subArray

Summary

  • Time Complexity: O(k)
  • Space Complexity: O(1)

This makes the Prefix solution highly efficient, especially when k is significantly smaller than the length of cardPoints.

func maxScore(cardPoints []int, k int) int {

	// tricky
	// PreFix Sum approach
	// Circular subarray
	// [1,2,3,4,5,6,1]

	// 123 561
	//     561 123

	// 561123
	//    123
	// ..112.
	// .611..
	// 561...

	totalSum := 0
	n := len(cardPoints)

	for i := 0; i < k; i++ {
		totalSum += cardPoints[i]
	}

	if len(cardPoints) == k {
		return totalSum
	}

	maxSum := totalSum
	for j := 0; j < k; j++ {

		totalSum -= cardPoints[k-1-j]
		totalSum += cardPoints[n-1-j]
		if maxSum < totalSum {
			maxSum = totalSum
		}
	}

	return maxSum
}

Please visit https: https://codeandalgo.com for more such contents

Leave a Reply

Your email address will not be published. Required fields are marked *