Ctrl K

Merge the Front Tiles

Hard
Tiles with integers on them lie in a row: the array tiles. Alice and Bob take turns, Alice first. While more than one tile is left, the player to move: - picks an integer x > 1 and removes the x leftmost tiles; - adds the sum of their numbers to their score; - puts a new tile carrying that sum at the front of the row. The game ends when one tile is left. Alice wants the difference (Alice's score) − (Bob's score) as large as possible, Bob wants it as small as possible. Return that difference when both play optimally.
Example 1
tiles=[3, -1, 4, -2] → 4

Alice merges every tile at once for 3 − 1 + 4 − 2 = 4, and Bob gets 0. Merging three tiles (+6) lets Bob merge the last two (+4) for a gap of 2; merging two (+2) lets Bob merge everything (+4) for a gap of −2.

Example 2
tiles=[-5, -6] → -11

There is a single move: Alice must merge both tiles and gets −11.

Example 3
tiles=[2, -3, 1] → 0

Merging everything gives a gap of 0. Merging the first two (−1) lets Bob merge the rest for 0, a gap of −1. Alice goes for 0.

Constraints
  • 2 ≤ len(tiles) ≤ 10⁵
  • -10⁴ ≤ tiles[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsThe Growing Grab Limit MediumGrab Up to Three Cards HardKeep the Lighter Half HardMouse, Cat and Cheese HardTotal Strength of All Squads Hard
loading Python…
Input
tiles=[3, -1, 4, -2]
Expected
4

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