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] <= 1000This approach uses dynamic programming along with pre-computed prefix sums to efficiently calculate the score differences.
We maintain a 2D DP table where dp[i][j] represents the maximum score difference the current player can achieve over the other player from index i to j. The prefix sums help in computing the sum of the stones between any two indices in constant time, which we require to decide which stone to remove to maximize the score difference.
In this C implementation, we start by computing the prefix sums to get the sum of elements between any two indices quickly. We then fill a 2D DP table, where each cell dp[i][j] holds the maximum score difference the current player can secure from stones in the subarray stones[i...j]. The scores for each move are calculated using the prefix sums, and we use the results to populate the DP table iteratively.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^2)
Space Complexity: O(n^2)
This approach leverages recursive backtracking with memoization to explore all possible game states and store previously computed results to prevent redundant calculations. Unlike the iterative DP approach, this recursive strategy keeps track of outcomes by diving directly into decision trees, thus exposing base-level game decisions before accumulating overall results with stored solutions.
This C function uses recursion and memoization to track possible score differences by evaluating potential moves recursively. Recursive calls are cached with memoization to avoid recalculating results, reducing time spent on previously solved sub-problems.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^2) (with memoization)
Space Complexity: O(n^2)
| Approach | Complexity |
|---|---|
| Approach 1: Dynamic Programming with Pre-computed Prefix Sum | Time Complexity: |
| Approach 2: Recursive with Memoization | Time Complexity: |
Jump Game VII - Leetcode 1871 - Python • NeetCode • 16,033 views views
Watch 9 more video solutions →Practice Stone Game VII with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor