1563. Stone Game V LeetCode Solution

In this guide, you will get 1563. Stone Game V LeetCode Solution with the best time and space complexity. The solution to Stone Game V problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Stone Game V solution in C++
  4. Stone Game V solution in Java
  5. Stone Game V solution in Python
  6. Additional Resources
1563. Stone Game V LeetCode Solution image

Problem Statement of Stone Game V

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. left row and right row), then Bob calculates the value of each row which is the sum of the values of all the stones in this row. Bob throws away the row which has the maximum value, and Alice’s score increases by the value of the remaining row. If the value of the two rows are equal, Bob lets Alice decide which row will be thrown away. The next round starts with the remaining row.
The game ends when there is only one stone remaining. Alice’s is initially zero.
Return the maximum score that Alice can obtain.

Example 1:

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.

See also  3093. Longest Common Suffix Queries LeetCode Solution

Example 2:

Input: stoneValue = [7,7,7,7,7,7,7]
Output: 28

Example 3:

Input: stoneValue = [4]
Output: 0

Constraints:

1 <= stoneValue.length <= 500
1 <= stoneValue[i] <= 106

Complexity Analysis

  • Time Complexity: O(n^3)
  • Space Complexity: O(n^2)

1563. Stone Game V LeetCode Solution in C++

class Solution {
 public:
  int stoneGameV(vector<int>& stoneValue) {
    const int n = stoneValue.size();
    vector<vector<int>> mem(n, vector<int>(n, INT_MIN));
    vector<int> prefix(n + 1);
    partial_sum(stoneValue.begin(), stoneValue.end(), prefix.begin() + 1);
    return stoneGameV(stoneValue, 0, n - 1, prefix, mem);
  }

 private:
  // Returns the maximum score that Alice can obtain from stoneValue[i..j].
  int stoneGameV(const vector<int>& stoneValue, int i, int j,
                 const vector<int>& prefix, vector<vector<int>>& mem) {
    if (i == j)
      return 0;
    if (mem[i][j] > 0)
      return mem[i][j];

    // Try all the possible partitions.
    for (int p = i; p < j; ++p) {
      // sum(stoneValue[i..p])
      const int leftSum = prefix[p + 1] - prefix[i];
      const int throwRight =
          leftSum + stoneGameV(stoneValue, i, p, prefix, mem);
      // sum(stoneValue[p + 1..j])
      const int rightSum = prefix[j + 1] - prefix[p + 1];
      const int throwLeft =
          rightSum + stoneGameV(stoneValue, p + 1, j, prefix, mem);
      if (leftSum < rightSum)  // Bob throws the right row.
        mem[i][j] = max(mem[i][j], throwRight);
      else if (leftSum > rightSum)  // Bob throws the left row.
        mem[i][j] = max(mem[i][j], throwLeft);
      else  // Alice decides which row to throw.
        mem[i][j] = max({mem[i][j], throwLeft, throwRight});
    }

    return mem[i][j];
  }
};
/* code provided by PROGIEZ */

1563. Stone Game V LeetCode Solution in Java

class Solution {
  public int stoneGameV(int[] stoneValue) {
    final int n = stoneValue.length;
    int[][] mem = new int[n][n];
    int[] prefix = new int[n + 1];
    Arrays.stream(mem).forEach(A -> Arrays.fill(A, Integer.MIN_VALUE));
    for (int i = 0; i < n; ++i)
      prefix[i + 1] = prefix[i] + stoneValue[i];
    return stoneGameV(stoneValue, 0, n - 1, prefix, mem);
  }

  // Returns the maximum score that Alice can obtain from stoneValue[i..j].
  private int stoneGameV(int[] stoneValue, int i, int j, int[] prefix, int[][] mem) {
    if (i == j)
      return 0;
    if (mem[i][j] != Integer.MIN_VALUE)
      return mem[i][j];

    // Try all the possible partitions.
    for (int p = i; p < j; ++p) {
      // sum(stoneValue[i..p])
      final int leftSum = prefix[p + 1] - prefix[i];
      final int throwRight = leftSum + stoneGameV(stoneValue, i, p, prefix, mem);
      // sum(stoneValue[p + 1..j])
      final int rightSum = prefix[j + 1] - prefix[p + 1];
      final int throwLeft = rightSum + stoneGameV(stoneValue, p + 1, j, prefix, mem);
      if (leftSum < rightSum)
        mem[i][j] = Math.max(mem[i][j], throwRight);
      else if (leftSum > rightSum)
        mem[i][j] = Math.max(mem[i][j], throwLeft);
      else
        mem[i][j] = Math.max(Math.max(mem[i][j], throwLeft), throwRight);
    }

    return mem[i][j];
  }
}
// code provided by PROGIEZ

1563. Stone Game V LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

See also  2614. Prime In Diagonal LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.