Ctrl K

Grab Up to Three Cards

Hard
Cards lie in a row, each showing an integer that may be negative: the array cards. Alice and Bob take turns, Alice first. On a turn, a player takes one, two or three cards from the front of the row and adds their numbers to their score. The game ends when no cards remain; both start with 0 points. Both play optimally, each trying to finish ahead of the other. Return "Alice" if Alice wins, "Bob" if Bob wins, and "Tie" if their scores are equal.
Example 1
cards=[4, -2, 5, 1] → 'Alice'

Alice takes three cards (4 − 2 + 5 = 7) and leaves Bob with 1. She cannot do better, and Bob cannot stop her.

Example 2
cards=[-3, 6, 2, 1] → 'Alice'

However Alice starts, she has to take the −3. Taking the first three cards gives her 5 and Bob gets 1, so she wins: the answer is Alice.

Example 3
cards=[5, -1, -1, 9] → 'Bob'

However many cards Alice takes, Bob takes the rest, the 9 included: 5 against 7, 4 against 8, or 3 against 9. Bob wins.

Constraints
  • 1 ≤ len(cards) ≤ 5 · 10⁴
  • -1000 ≤ cards[i] ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsKeep the Lighter Half HardMouse, Cat and Cheese HardMerge the Front Tiles HardDuel for the End Cards MediumDuel for the End Heaps Medium
loading Python…
Input
cards=[4, -2, 5, 1]
Expected
'Alice'

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