Ctrl K

Duel for the End Cards

Medium
A row of cards with numbers cards lies on the table. Two players take turns, and the first player starts. On a turn a player takes one card from either end of the row, the leftmost or the rightmost, and adds its number to their score. The game ends when no cards are left. Both play optimally. Return True if the first player ends with at least as many points as the second (a tie also counts as a win for them), and False otherwise.
Example 1
cards=[3, 9, 1, 2] → True

The first player takes 2. The second is left with 3 and 1 at the ends; whichever they take, the first player grabs 9. The first player ends with at least 11, the second with at most 4.

Example 2
cards=[2, 7, 3] → False

Whichever end the first player takes, the second grabs 7. The first player gets 2 + 3 = 5 < 7.

Example 3
cards=[4, 4] → True
Constraints
  • 1 ≤ len(cards) ≤ 20
  • 0 ≤ cards[i] ≤ 10⁷
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsDuel for the End Heaps MediumThe Growing Grab Limit MediumScoring What Remains MediumTake From Any Pile MediumGrab Up to Three Cards Hard
loading Python…
Input
cards=[3, 9, 1, 2]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 112 tests on the server, hidden ones included.