forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1140.go
44 lines (38 loc) · 828 Bytes
/
1140.go
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
44
const INT_MAX = int(^uint(0) >> 1)
const INT_MIN = ^INT_MAX
var mem [][]int
var n int
func stoneGameII(piles []int) int {
n = len(piles)
mem = make([][]int, n)
for i := 0; i < n; i++ {
mem[i] = make([]int, n)
for j := 0; j < n; j++ {
mem[i][j] = INT_MAX
}
}
for i := n - 2; i >= 0; i-- {
piles[i] += piles[i + 1]
}
return dfs(0, 1, &piles)
}
func dfs(i, m int, piles *[]int) int {
if mem[i][m] != INT_MAX {
return mem[i][m]
}
if i + 2 *m >= n {
return (*piles)[i]
}
res := INT_MIN
for x := 1; x <= 2 * m; x++ {
res = max(res, (*piles)[i] - dfs(i + x, max(x, m), piles))
}
mem[i][m] = res
return res
}
func max(a, b int) int {
if a > b {
return a
}
return b
}