All problems
Maximum Points from Cards
Problem
Given card_points in a row, return the maximum score from taking exactly k cards, each from the left or right end of the remaining row.
The taken cards always form the complement of one contiguous window of size n - k, so subtract the minimum window sum from the total. Return the total when k covers the whole row.
Hint
Maximising the ends equals minimising the contiguous middle that is left behind.
Test cases
case 1
in: [[1,2,3,4,5,6,1],3]
expected: 12
case 2
in: [[2,2,2],2]
expected: 4
case 3
in: [[9,7,7,9,7,7,9],7]
expected: 55
case 4
in: [[1,1000,1],1]
expected: 1
case 5
in: [[5],1]
expected: 5
Starter codePython
def max_points_cards(card_points, k):
# Your code here
pass5 test cases · run your code against them in the editor