aboutsummaryrefslogtreecommitdiff
path: root/stone_game2.c
blob: ed142e9203faf56af6fb7b53699690fddf15c1a8 (plain)
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
41
42
43
// 1140. Stone Game II
#include "leetcode.h"

/*
 * alice and bob continue their games with piles of stones. there are a number
 * of piles arranged in a row, and each pile has a positive integer number of
 * stones 'piles[i]'. the objective of the game is to end with the most stones.
 * alice and bob take turns, with alice starting first. initiall, 'M = 1'. on
 * each player's turn, that player can take all the stones on the 'X' remaining
 * piles, where '1 <= X <= 2M'. then, we set 'M = max(M, X)'. the game continues
 * until all the stones have been take. assuming alice and bob play optimally,
 * return maximum stones alice can get.
 */

int stoneGameII(int *piles, int piles_size) {
  int n = piles_size;
  int *sum = (int *)malloc((n + 1) * sizeof(int));
  int **dp = (int **)malloc((n + 1) * sizeof(int *));
  for (int i = 0; i <= n; i++)
    dp[i] = (int *)calloc(n + 1, sizeof(int));
  sum[n] = 0;
  for (int i = n - 1; i >= 0; i--)
    sum[i] = sum[i + 1] + piles[i];
  for (int i = 0; i < n; ++i)
    dp[i][n] = sum[i];
  for (int i = n - 1; i >= 0; i--)
    for (int m = n - 1; m >= 1; m--)
      for (int x = 1; x <= 2 * m && i + x <= n; ++x)
        dp[i][m] = fmax(dp[i][m], sum[i] - dp[i + x][(int)fmax(m, x)]);
  int ans = dp[0][1];
  for (int i = 0; i <= n; i++)
    free(dp[i]);
  free(dp);
  free(sum);
  return ans;
}

int main() {
  int p1[] = {2, 7, 9, 4, 4};
  int p2[] = {1, 2, 3, 4, 5, 100};
  printf("%d\n", stoneGameII(p1, 5)); // expect: 10
  printf("%d\n", stoneGameII(p2, 6)); // expect: 104
}