Problem
Alice and Bob continue their games with piles of stones. There are a number of piles arranged in a row, and each pile has a positive integer number of stones piles[i]
. The objective of the game is to end with the most stones.
Alice and Bob take turns, with Alice starting first. Initially, M = 1
.
On each player’s turn, that player can take all the stones in the first X
remaining piles, where 1 <= X <= 2M
. Then, we set M = max(M, X)
.
The game continues until all the stones have been taken.
Assuming Alice and Bob play optimally, return the maximum number of stones Alice can get.
Examples
Example 1:
|
|
Example 2:
|
|
Solution
Method 1 - By minimizing the stones returned by Bob
The game involves Alice and Bob playing optimally to collect as many stones as possible from a row of piles. Alice always plays first, and the players take turns. The key constraints are:
-
Each player can take any number of piles between 1 and 2M, where M is initially set to 1 and adjusts based on the moves.
-
The goal is to maximize the number of stones collected by Alice.
-
Prefix Sums:
- We use
prefixSums
to quickly calculate the sum of stones from any subarray. prefixSums[i]
stores the total number of stones from the start up to the i-th pile.
- We use
-
Memoization Table:
memo[i][M]
stores the maximum number of stones Alice can collect starting from indexi
with the parameterM
.- This avoids recalculating results for the same state multiple times.
-
Recursive Function:
- The helper function
findMaxStones
handles the game logic recursively. - It ensures that both players play optimally, and calculates Alice’s maximum possible stones.
- The helper function
This is how recursion will work:
- The goal is to maximize Alice’s collected stones. For each possible number of piles
X
Alice can take (from 1 to2 * M
):- Calculate the total stones Alice can collect by summing the stones from index
i
toi + X - 1
. - Recursively compute the result for the remaining piles starting from index
i + X
, and adjustM
tomax(M, X)
. - Deduct the maximum stones Bob can collect in his turn from the total stones Alice can collect.
- Calculate the total stones Alice can collect by summing the stones from index
Video Explanation
Here is the video explanation:
Code
|
|
Complexity
- Time:
O(n^3)
- Because at each recursive call,
i
goes from 0 to n - But another parameter is
m
and in worst case it can go ton/2
- Then, we are running the inner loop insider function from X = 1 to n in worst case
- Because at each recursive call,
- Space:
O(n^2)
using recursion stack and memo table
Method 2 - By maximizing the difference
Code
|
|
Complexity
- Time:
O(n^2)
, , where n is the number of piles.- There are
O(n²)
unique states for(i, M)
:i
ranges from 0 ton-1
(start index of remaining piles).M
ranges from 1 ton
(maximum value for M is n).
- For each state, the inner loop runs at most
O(n)
times (since X can go up to2M
, butM ≤ n
). - However, due to memoization, each state is computed only once, so the total number of recursive calls is
O(n²)
. - Thus, the overall time complexity is
O(n²)
.
- There are
- Space:
O(n^2)
- The memoization table
memo
is of sizen × (n+1): O(n²)
. - The prefix sum array is
O(n)
. - The recursion stack can go up to
O(n)
in depth.
- The memoization table