Ctrl K

Scoring What Remains

Medium
Tiles lie in a row, and tile i carries the number tiles[i]. Two players take turns, the first player moving first. On a turn, a player removes an end tile, either the leftmost or the rightmost, and scores the sum of the numbers on the tiles **still in the row** after the move. The game ends when no tiles are left. The second player knows they cannot win, so they try to keep the score gap as small as possible, while the first player tries to make it as large as possible. Both play optimally. Return the final gap: the first player's score minus the second player's.
Example 1
tiles=[2, 7, 3] → 3

The first player removes 2 and scores 7 + 3 = 10. The second does best by removing 3 for 7 points, then the first removes 7 for 0. The gap is 10 − 7 = 3; any other opening gives the first player less.

Example 2
tiles=[4, 4] → 4

The first player removes either tile for 4 points, and the second removes the last one for 0.

Example 3
tiles=[6, 1, 5, 2] → 11
Constraints
  • 2 ≤ len(tiles) ≤ 1000
  • 1 ≤ tiles[i] ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsDuel for the End Cards MediumDuel for the End Heaps MediumThe Growing Grab Limit MediumTake From Any Pile MediumGrab Up to Three Cards Hard
loading Python…
Input
tiles=[2, 7, 3]
Expected
3

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