Skip to the code
cf.
+25 XP

#131 Best Picks from Both Ends

medium

A carousel shows reward cards in a row, worth points[i] each. A player may take exactly k cards, but each card must be taken from the left end or the right end of what's left of the row.

Write bestPicks(points, k) that returns the largest total the player can collect. k is never more than the number of cards.

Example 1

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

Take the three cards on the right: 1 + 6 + 5 = 12.

Example 2

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

Taking every card gives the total, 55.

Constraints

  • 1 <= k <= points.length <= 100000
  • 1 <= points[i] <= 10^4
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).