InferenceAI

Enter at least two characters to search.

Start free

Cards From the Ends

mediumSliding Window25 min25 XP

Cards are in a row with values points. In each of k moves you take one card from the left end or the right end. Return the maximum total you can collect.

Requirements

  • O(n)O(n): equivalently, find the minimum-sum window of length n−kn - k that you leave behind.

Examples

EXAMPLE WALKTHROUGHtake from the right

A guided view of the published example, not a trace of your code.

INPUT[[1,2,3,4,5,6,1],3]
TASKcards_from_the_ends()
EXPECTED OUTPUT12

Start with the input values shown in this example.

Example 1 of 3

Input: points = [1,2,3,4,5,6,1], k = 3

Output: 12

Explanation: take from the right

Input: points = [2,2,2], k = 2

Output: 4

Explanation: equal

Input: points = [9,7,7,9,7,7,9], k = 7

Output: 55

Explanation: take all

Hint 1

Start by taking the first k cards, then swap them one at a time for cards from the right end.

Topics

DSASliding WindowPython