Advertisement

Stone Game VII - LeetCode 1690 Solution

Stone Game VII - Complete Solution Guide

Stone Game VII is LeetCode problem 1690, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 . Al

Detailed Explanation

The Stone Game VII presents a game between Alice and Bob where they remove stones from either end of a row. Each player's score is the sum of the remaining stones after their removal. Alice aims to maximize the difference between her score and Bob's, while Bob wants to minimize it. The goal is to determine the optimal score difference Alice can achieve, assuming both players play perfectly. The input is an array `stones` representing the values of the stones. The output is the maximum possible score difference between Alice and Bob.

Solution Approach

The provided code implements a dynamic programming solution. It calculates a prefix sum array to efficiently compute the sum of stone values in a range. A 2D DP table `dp[i][j]` stores the maximum score difference Alice can achieve when playing optimally on the subarray `stones[i...j]`. The table is filled in order of increasing subarray length, from length 2 up to n. For each subarray, Alice has two choices: remove the leftmost stone or the rightmost stone. The `dp[i][j]` value is then the maximum of (sum of remaining stones - dp[i+1][j]) and (sum of remaining stones - dp[i][j-1]), representing the optimal choice Alice can make. The final result is stored in `dp[0][n-1]`.

Step-by-Step Algorithm

  1. Step 1: Calculate the prefix sum array `prefix`. `prefix[i]` stores the sum of the first `i` stones.
  2. Step 2: Initialize a 2D DP table `dp` of size n x n with all values set to 0. `dp[i][j]` will store the maximum score difference for the subarray stones[i...j].
  3. Step 3: Iterate over the length of the subarrays from 2 to n. This is the outer loop: `for length in range(2, n + 1)`
  4. Step 4: For each length, iterate over all possible starting positions `i` of the subarray. This is the inner loop: `for i in range(n - length + 1)`
  5. Step 5: Calculate the ending position `j` of the subarray: `j = i + length - 1`
  6. Step 6: Calculate the sum of the stones in the subarray [i+1...j] if removing the leftmost stone: `sum_without_left = prefix[j + 1] - prefix[i + 1]`
  7. Step 7: Calculate the sum of the stones in the subarray [i...j-1] if removing the rightmost stone: `sum_without_right = prefix[j] - prefix[i]`
  8. Step 8: Calculate the score difference if removing the leftmost stone: `option1 = sum_without_left - dp[i + 1][j]`
  9. Step 9: Calculate the score difference if removing the rightmost stone: `option2 = sum_without_right - dp[i][j - 1]`
  10. Step 10: Store the maximum of `option1` and `option2` in `dp[i][j]`. This represents Alice's optimal choice for this subarray: `dp[i][j] = max(option1, option2)`
  11. Step 11: After the loops complete, return `dp[0][n - 1]`, which represents the maximum score difference for the entire array.

Key Insights

  • Insight 1: Dynamic Programming is crucial. The optimal strategy for a sub-array depends on the optimal strategies for its sub-sub-arrays.
  • Insight 2: The prefix sum helps in calculating the sum of a subarray in O(1) time, avoiding repeated calculations within the DP.
  • Insight 3: The core recurrence relation relates the optimal score difference for a sub-array to the optimal score differences of sub-arrays formed by removing the left or right element.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(n^2)

Topics

This problem involves: Array, Math, Dynamic Programming, Game Theory.

Companies

Asked at: Dunzo.