Maximum Points You Can Obtain from Cards
January 26, 2024
Problem Statement #
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 k
steps to take 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:
- Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: Take the last three cards and your points are 1 + 5 + 6 = 12.
Solution Approach #
The solution involves finding the smallest subarray length n-k
where the sum is minimum. The maximum score can be obtained by subtracting this minimum sum from the total sum of the array.
Algorithm Steps #
- Calculate the total sum of the array
cardPoints
. - Find the subarray of length
n-k
that has the minimum sum. - The maximum score is the total sum minus the minimum sum of this subarray.
- Return the maximum score.
Code (Python) #
def maxScore(cardPoints, k):
n = len(cardPoints)
total_sum = sum(cardPoints)
if n == k:
return total_sum
min_subarray_sum = float('inf')
current_sum = 0
for i in range(n - k - 1):
current_sum += cardPoints[i]
for i in range(n - k - 1, n):
current_sum += cardPoints[i]
min_subarray_sum = min(min_subarray_sum, current_sum)
current_sum -= cardPoints[i - (n - k - 1)]
return total_sum - min_subarray_sum
# Test the function
print(maxScore([1,2,3,4,5,6,1], 3)) # Output: 12
Breakdown of the Algorithm in the Solution: #
-
Total Sum Calculation: First, the total sum of all elements in
cardPoints
is calculated. This is a straightforward summation, not part of the sliding window technique. -
Finding Minimum Subarray Sum:
- The core of the solution involves finding the subarray of length
n - k
(wheren
is the length ofcardPoints
andk
is the number of cards you can take) that has the minimum sum. This is where the sliding window algorithm is applied. - Initially, the sum of the first
n - k
elements is calculated. This forms the initial window. - Then, the window is “slid” across the array: for each new position of the window, the sum is updated by subtracting the element that is left behind and adding the new element that comes into the window.
- During this process, the minimum sum of any such window is tracked.
- The core of the solution involves finding the subarray of length
-
Calculating Maximum Score:
- The maximum score is then calculated by subtracting this minimum subarray sum from the total sum. The logic here is that by minimizing the sum of the elements you don’t take, you maximize the sum of the elements you do take.
Why Sliding Window? #
- Efficiency: The sliding window algorithm is ideal for problems where you need to calculate a property (like a sum) of subsets of contiguous elements in an array. It allows for an O(n) solution by avoiding redundant calculations.
- Applicability: In this problem, the sliding window helps efficiently find the subarray that minimizes the sum of the cards not taken, which directly relates to maximizing the sum of the cards taken.
In summary, the sliding window algorithm is adeptly used in this solution to find the minimum sum subarray, which is key to calculating the maximum points obtainable from the given set of cards.
Time Complexity #
O(n), where n is the number of cards. The array is processed in a single pass.
Space Complexity #
O(1), as only a constant amount of extra space is used.