Skip to content

1563. Stone Game V 👍

  • Time: $O(n^3)$
  • Space: $O(n^2)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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];
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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];
  }
}