Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Alice and Bob take turns playing a game, with Alice starting first.
There are n stones arranged in a row. On each player's turn, they can remove either the leftmost stone or the rightmost stone from the row and receive points equal to the sum of the remaining stones' values in the row. The winner is the one with the higher score when there are no stones left to remove.
Bob found that he will always lose this game (poor Bob, he always loses), so he decided to minimize the score's difference. Alice's goal is to maximize the difference in the score.
Given an array of integers stones where stones[i] represents the value of the ith stone from the left, return the difference in Alice and Bob's score if they both play optimally.
Example 1:
Input: stones = [5,3,1,4,2] Output: 6 Explanation: - Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4]. - Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4]. - Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4]. - Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4]. - Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = []. The score difference is 18 - 12 = 6.
Example 2:
Input: stones = [7,90,5,1,100,10,10,2] Output: 122
Constraints:
n == stones.length2 <= n <= 10001 <= stones[i] <= 1000Problem summary: Alice and Bob take turns playing a game, with Alice starting first. There are n stones arranged in a row. On each player's turn, they can remove either the leftmost stone or the rightmost stone from the row and receive points equal to the sum of the remaining stones' values in the row. The winner is the one with the higher score when there are no stones left to remove. Bob found that he will always lose this game (poor Bob, he always loses), so he decided to minimize the score's difference. Alice's goal is to maximize the difference in the score. Given an array of integers stones where stones[i] represents the value of the ith stone from the left, return the difference in Alice and Bob's score if they both play optimally.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Dynamic Programming
[5,3,1,4,2]
[7,90,5,1,100,10,10,2]
stone-game)stone-game-ii)stone-game-iii)stone-game-iv)stone-game-v)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1690: Stone Game VII
class Solution {
private int[] s;
private Integer[][] f;
public int stoneGameVII(int[] stones) {
int n = stones.length;
s = new int[n + 1];
f = new Integer[n][n];
for (int i = 0; i < n; ++i) {
s[i + 1] = s[i] + stones[i];
}
return dfs(0, n - 1);
}
private int dfs(int i, int j) {
if (i > j) {
return 0;
}
if (f[i][j] != null) {
return f[i][j];
}
int a = s[j + 1] - s[i + 1] - dfs(i + 1, j);
int b = s[j] - s[i] - dfs(i, j - 1);
return f[i][j] = Math.max(a, b);
}
}
// Accepted solution for LeetCode #1690: Stone Game VII
func stoneGameVII(stones []int) int {
n := len(stones)
s := make([]int, n+1)
f := make([][]int, n)
for i, x := range stones {
s[i+1] = s[i] + x
f[i] = make([]int, n)
}
var dfs func(int, int) int
dfs = func(i, j int) int {
if i > j {
return 0
}
if f[i][j] != 0 {
return f[i][j]
}
a := s[j+1] - s[i+1] - dfs(i+1, j)
b := s[j] - s[i] - dfs(i, j-1)
f[i][j] = max(a, b)
return f[i][j]
}
return dfs(0, n-1)
}
# Accepted solution for LeetCode #1690: Stone Game VII
class Solution:
def stoneGameVII(self, stones: List[int]) -> int:
@cache
def dfs(i: int, j: int) -> int:
if i > j:
return 0
a = s[j + 1] - s[i + 1] - dfs(i + 1, j)
b = s[j] - s[i] - dfs(i, j - 1)
return max(a, b)
s = list(accumulate(stones, initial=0))
ans = dfs(0, len(stones) - 1)
dfs.cache_clear()
return ans
// Accepted solution for LeetCode #1690: Stone Game VII
struct Solution;
impl Solution {
fn stone_game_vii(stones: Vec<i32>) -> i32 {
let n = stones.len();
let mut memo: Vec<Vec<i32>> = vec![vec![0; n]; n];
let sum = stones.iter().sum();
Self::dp(0, n - 1, &mut memo, &stones, sum)
}
fn dp(l: usize, r: usize, memo: &mut Vec<Vec<i32>>, stones: &[i32], sum: i32) -> i32 {
if l == r {
0
} else {
if memo[l][r] != 0 {
memo[l][r]
} else {
let mut res = 0;
res = res.max(sum - stones[l] - Self::dp(l + 1, r, memo, stones, sum - stones[l]));
res = res.max(sum - stones[r] - Self::dp(l, r - 1, memo, stones, sum - stones[r]));
memo[l][r] = res;
res
}
}
}
}
#[test]
fn test() {
let stones = vec![5, 3, 1, 4, 2];
let res = 6;
assert_eq!(Solution::stone_game_vii(stones), res);
let stones = vec![7, 90, 5, 1, 100, 10, 10, 2];
let res = 122;
assert_eq!(Solution::stone_game_vii(stones), res);
}
// Accepted solution for LeetCode #1690: Stone Game VII
function stoneGameVII(stones: number[]): number {
const n = stones.length;
const s: number[] = Array(n + 1).fill(0);
for (let i = 0; i < n; ++i) {
s[i + 1] = s[i] + stones[i];
}
const f: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
const dfs = (i: number, j: number): number => {
if (i > j) {
return 0;
}
if (f[i][j]) {
return f[i][j];
}
const a = s[j + 1] - s[i + 1] - dfs(i + 1, j);
const b = s[j] - s[i] - dfs(i, j - 1);
return (f[i][j] = Math.max(a, b));
};
return dfs(0, n - 1);
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.