Skip to content

Practice Stone Game VII

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1690
LeetCode Problem

Stone Game VII

Problem Statement

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.

Source: Stone Game VII on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
stones = [5,3,1,4,2]
Output
6

# Example 2

Input
stones = [7,90,5,1,100,10,10,2]
Output
122

Constraints

n == stones.length
2 <= n <= 1000
1 <= stones[i] <= 1000

Related LeetCode Problems

Practice more problems similar to Stone Game VII.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.