aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--stone_game2.c10
-rw-r--r--stone_game2.py42
2 files changed, 46 insertions, 6 deletions
diff --git a/stone_game2.c b/stone_game2.c
index b1b2092..ed142e9 100644
--- a/stone_game2.c
+++ b/stone_game2.c
@@ -1,7 +1,5 @@
// 1140. Stone Game II
-#include <math.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include "leetcode.h"
/*
* alice and bob continue their games with piles of stones. there are a number
@@ -16,10 +14,10 @@
int stoneGameII(int *piles, int piles_size) {
int n = piles_size;
- int *sum = malloc((n + 1) * sizeof(int));
- int **dp = malloc((n + 1) * sizeof(int *));
+ 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] = calloc(n + 1, sizeof(int));
+ 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];
diff --git a/stone_game2.py b/stone_game2.py
new file mode 100644
index 0000000..46ead18
--- /dev/null
+++ b/stone_game2.py
@@ -0,0 +1,42 @@
+# 1140. Stone Game II
+
+"""
+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.
+"""
+
+
+class Solution(object):
+ def stoneGameII(self, piles):
+ """
+ :type piles: List[int]
+ :rtype: int
+ """
+
+ def dp(x, y, z):
+ if y == len(piles):
+ return 0
+ if x == 0:
+ curr, res = 0, float("-inf")
+ for i in range(1, min(z * 2, len(piles) - y) + 1):
+ curr += piles[y + i - 1]
+ res = max(res, curr + dp(1, y + i, max(i, z)))
+ else:
+ res = float("inf")
+ for i in range(1, min(z * 2, len(piles) - y) + 1):
+ res = min(res, dp(0, y + i, max(i, z)))
+ return res
+
+ return dp(0, 0, 1)
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.stoneGameII(piles=[2, 7, 9, 4, 4]))
+ print(obj.stoneGameII(piles=[1, 2, 3, 4, 5, 100]))