Skip to content

1690. Stone Game VII 👍

Approach 1: Top-down

  • Time: $O(n^2)$
  • 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
class Solution {
 public:
  int stoneGameVII(vector<int>& stones) {
    const int n = stones.size();
    // dp[i][j] := max score you can get more than your opponent in stones[i..j]
    dp.resize(n, vector<int>(n));
    prefix.resize(n + 1);

    partial_sum(begin(stones), end(stones), begin(prefix) + 1);
    return stoneGameVII(stones, 0, n - 1);
  }

 private:
  vector<vector<int>> dp;
  vector<int> prefix;

  int stoneGameVII(const vector<int>& stones, int i, int j) {
    if (i == j)
      return 0;
    if (dp[i][j] > 0)
      return dp[i][j];

    dp[i][j] =
        max({dp[i][j],
             // remove stones[i], so get sum of stones[i + 1..j]
             prefix[j + 1] - prefix[i + 1] - stoneGameVII(stones, i + 1, j),
             // remove stones[j], so get sum of stones[i..j - 1]
             prefix[j] - prefix[i] - stoneGameVII(stones, i, j - 1)});

    return dp[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
class Solution {
  public int stoneGameVII(int[] stones) {
    final int n = stones.length;
    // dp[i][j] := max score you can get more than your opponent in stones[i..j]
    dp = new int[n][n];
    prefix = new int[n + 1];

    for (int i = 0; i < n; ++i)
      prefix[i + 1] = stones[i] + prefix[i];

    return stoneGameVII(stones, 0, n - 1);
  }

  private int[][] dp;
  private int[] prefix;

  int stoneGameVII(int[] stones, int i, int j) {
    if (i == j)
      return 0;
    if (dp[i][j] > 0)
      return dp[i][j];

    dp[i][j] =
        Math.max(dp[i][j], Math.max(
                               // remove stones[i], so get sum of stones[i + 1..j]
                               prefix[j + 1] - prefix[i + 1] - stoneGameVII(stones, i + 1, j),
                               // remove stones[j], so get sum of stones[i..j - 1]
                               prefix[j] - prefix[i] - stoneGameVII(stones, i, j - 1)));

    return dp[i][j];
  }
}

Approach 2: Bottom-up

  • Time: $O(n^2)$
  • Space: $O(n^2)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
 public:
  int stoneGameVII(vector<int>& stones) {
    const int n = stones.size();
    // dp[i][j] := max score you can get more than your opponent in stones[i..j]
    vector<vector<int>> dp(n, vector<int>(n));
    vector<int> prefix(n + 1);

    partial_sum(begin(stones), end(stones), begin(prefix) + 1);

    for (int d = 1; d < n; ++d)
      for (int i = 0; i + d < n; ++i) {
        const int j = i + d;
        dp[i][j] = max(prefix[j + 1] - prefix[i + 1] - dp[i + 1][j],
                       prefix[j] - prefix[i] - dp[i][j - 1]);
      }

    return dp[0][n - 1];
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
  public int stoneGameVII(int[] stones) {
    final int n = stones.length;
    // dp[i][j] := max score you can get more than your opponent in stones[i..j]
    int[][] dp = new int[n][n];
    int[] prefix = new int[n + 1];

    for (int i = 0; i < n; ++i)
      prefix[i + 1] = stones[i] + prefix[i];

    for (int d = 1; d < n; ++d)
      for (int i = 0; i + d < n; ++i) {
        final int j = i + d;
        dp[i][j] = Math.max(prefix[j + 1] - prefix[i + 1] - dp[i + 1][j],
                            prefix[j] - prefix[i] - dp[i][j - 1]);
      }

    return dp[0][n - 1];
  }
}
Back to top