-
Notifications
You must be signed in to change notification settings - Fork 0
/
stone-game-iii.cpp
54 lines (48 loc) · 1.28 KB
/
stone-game-iii.cpp
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
45
46
47
48
49
50
51
52
53
54
class Solution
{
public:
static const int N = 5e4 + 5;
int dp[N][2];
int solve(int index, bool turn, vector<int> &stoneValue)
{
if (index >= stoneValue.size())
{
return 0;
}
if (dp[index][turn] != -1)
{
return dp[index][turn];
}
if (!turn)
{
int s = 0;
dp[index][turn] = -1e8;
for (int i = 0; i < 3 && i + index < stoneValue.size(); i++)
{
s += stoneValue[index + i];
dp[index][turn] = max(dp[index][turn], solve(index + i + 1, 1 - turn, stoneValue) + s);
}
}
else
{
int s = 0;
dp[index][turn] = 1e8;
for (int i = 0; i < 3 && i + index < stoneValue.size(); i++)
{
s += stoneValue[index + i];
dp[index][turn] = min(dp[index][turn], solve(index + i + 1, 1 - turn, stoneValue) - s);
}
}
return dp[index][turn];
}
string stoneGameIII(vector<int> &stoneValue)
{
memset(dp, -1, sizeof dp);
int ans = solve(0, 0, stoneValue);
if (ans > 0)
return "Alice";
if (ans < 0)
return "Bob";
return "Tie";
}
};