forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1140.cpp
More file actions
29 lines (28 loc) · 702 Bytes
/
1140.cpp
File metadata and controls
29 lines (28 loc) · 702 Bytes
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
class Solution
{
public:
int stoneGameII(vector<int>& piles)
{
n = piles.size();
for (int i = n - 2; i >= 0; --i)
{
piles[i] += piles[i+1];
}
mem = vector<vector<int>>(n, vector<int>(n, INT_MAX));
return dfs(0, 1, piles);
}
vector<vector<int>> mem;
int n;
int dfs(int i, int m, vector<int>& piles)
{
if (mem[i][m] != INT_MAX) return mem[i][m];
if (i + 2 * m >= n) return piles[i];
int res = INT_MIN;
for (int x = 1; x <= 2 * m; ++x)
{
res = max(res, piles[i] - dfs(i + x, max(x, m), piles));
}
mem[i][m] = res;
return res;
}
};