Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1563: Stone Game V

In this guide, we solve Leetcode #1563 Stone Game V in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Array, Math, Dynamic Programming, Game Theory

Intuition

The problem breaks into overlapping subproblems, so caching results prevents exponential repetition.

A carefully chosen DP state captures exactly what we need to build the final answer.

Approach

Define the DP state and recurrence, then compute states in the correct order.

Optionally compress space once the recurrence is clear.

Steps:

  • Choose a DP state definition.
  • Write the recurrence and base cases.
  • Compute states in the correct order.

Example

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.

Python Solution

def max(a: int, b: int) -> int: return a if a > b else b class Solution: def stoneGameV(self, stoneValue: List[int]) -> int: @cache def dfs(i: int, j: int) -> int: if i >= j: return 0 ans = l = 0 r = s[j + 1] - s[i] for k in range(i, j): l += stoneValue[k] r -= stoneValue[k] if l < r: if ans >= l * 2: continue ans = max(ans, l + dfs(i, k)) elif l > r: if ans >= r * 2: break ans = max(ans, r + dfs(k + 1, j)) else: ans = max(ans, max(l + dfs(i, k), r + dfs(k + 1, j))) return ans s = list(accumulate(stoneValue, initial=0)) return dfs(0, len(stoneValue) - 1)

Complexity

The time complexity is O(n3)O(n^3)O(n3), and the space complexity is O(n2)O(n^2)O(n2). The space complexity is O(n2)O(n^2)O(n2).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy