-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1140-Stone-Game-II.cpp
More file actions
36 lines (30 loc) · 844 Bytes
/
1140-Stone-Game-II.cpp
File metadata and controls
36 lines (30 loc) · 844 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
30
31
32
33
34
35
36
class Solution
{
public:
int go( int i, int M, bool Alice, vector<int>& piles )
{
if( i >= piles.size() )
return 0;
if( dp[i][M][Alice] != -1 )
return dp[i][M][Alice];
int ans = (Alice? 0 : 1e9), sum = 0;
for( int step = 1; step <= 2 * M; step++ )
{
if( step + i - 1 == piles.size() )
break;
sum += piles[i + step - 1];
if( Alice )
ans = max(ans, go(i + step, max(M, step), !Alice, piles) + sum);
else
ans = min(ans, go(i + step, max(M, step), !Alice, piles));
}
return dp[i][M][Alice] = ans;
}
int stoneGameII(vector<int>& piles)
{
memset(dp, -1, sizeof dp);
return go(0, 1, 1, piles);
}
private:
int dp[105][305][2];
};