Stone Game V Problem
Description
LeetCode Problem 1563.
There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
In each round of the game, Alice divides the row into two non-empty rows (i.e. left row and right row), then Bob calculates the value of each row which is the sum of the values of all the stones in this row. Bob throws away the row which has the maximum value, and Alice's score increases by the value of the remaining row. If the value of the two rows are equal, Bob lets Alice decide which row will be thrown away. The next round starts with the remaining row.
The game ends when there is only one stone remaining. Alice's score is initially zero.
Return the maximum score that Alice can obtain.
Example 1:
Input: stoneValue = [6,2,3,4,5,5]
Output: 18
Explanation: In the first round, Alice divides the row to [6,2,3], [4,5,5]. The left row has the value 11 and the right row has value 14. Bob throws away the right row and Alice's score is now 11.
In the second round Alice divides the row to [6], [2,3]. This time Bob throws away the left row and Alice's score becomes 16 (11 + 5).
The last round Alice has only one choice to divide the row which is [2], [3]. Bob throws away the right row and Alice's score is now 18 (16 + 2). The game ends because only one stone is remaining in the row.
Example 2:
Input: stoneValue = [7,7,7,7,7,7,7]
Output: 28
Example 3:
Input: stoneValue = [4]
Output: 0
Constraints:
1 <= stoneValue.length <= 5001 <= stoneValue[i] <= 10^6
Solution
Approach: Interval Memoization with Pruning
After each split, the game continues on a contiguous part of the original row, so the state is an interval [i, j]. Let best(i, j) be Alice's best score on stoneValue[i..j]; a single stone gives 0. With prefix sums, the values of the two parts of every split are available in O(1).
For each split point k, with left part [i..k] worth left and right part [k+1..j] worth right:
left < right: Bob throws away the right part, Alice gainsleftand continues on the left:left + best(i, k).left > right: symmetric,right + best(k + 1, j).left == right: Alice chooses, so take the better of the two.
best(i, j) is the maximum over all k, and the answer is best(0, n - 1).
Plain memoization is O(n3). Two cheap prunings make it much faster in practice, based on the fact that the remaining part can add at most its own value again (Alice's future score on a part worth s is less than s):
- If
left < rightandresult >= 2 * left, this split cannot beat the current result: skip it. - If
left > rightandresult >= 2 * right, every later split has an even smaller right part, so stop the loop.
Complexity: O(n3) time in the worst case (far less with the pruning) and O(n2) extra space for the memo.
from functools import lru_cache
class Solution:
def stoneGameV(self, stoneValue: List[int]) -> int:
n = len(stoneValue)
prefix = [0] * (n + 1)
for i, v in enumerate(stoneValue):
prefix[i + 1] = prefix[i] + v
@lru_cache(None)
def best(i: int, j: int) -> int:
"""Alice's best score on the row stoneValue[i..j]."""
if i == j:
return 0
result = 0
left, right = 0, prefix[j + 1] - prefix[i]
for k in range(i, j): # split into [i..k] and [k+1..j]
left += stoneValue[k]
right -= stoneValue[k]
if left < right:
# Alice keeps the left part; it can add at most `left` more, so skip if that cannot win.
if result < 2 * left:
result = max(result, left + best(i, k))
elif left > right:
# Alice keeps the right part; it only shrinks from here on, so stop once it cannot win.
if result >= 2 * right:
break
result = max(result, right + best(k + 1, j))
else:
result = max(result, left + max(best(i, k), best(k + 1, j)))
return result
return best(0, n - 1)