-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1406-stone-game-iii.cpp
43 lines (33 loc) · 1016 Bytes
/
1406-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
class Solution {
public:
vector<int>dp;
int n;
const int INF = -0x3f3f3f3f;
//This recursive function mainly calculates the maximum profit that alice can make
int recurse(vector<int> &arr, int idx)
{
if(idx>=n)
return 0;
if(dp[idx]!=INF)
return dp[idx];
int op1 = INT_MIN , op2 = INT_MIN , op3 = INT_MIN;
op1 = arr[idx] - recurse(arr,idx+1);
if(idx+1<n)
op2 = arr[idx]+arr[idx+1]-recurse(arr,idx+2);
if(idx+2<n)
op3 = arr[idx]+arr[idx+1]+arr[idx+2]-recurse(arr,idx+3);
return dp[idx] = max({op1,op2,op3});
}
string stoneGameIII(vector<int>& arr)
{
n = arr.size();
dp.resize(n+1,INF);
int profit = recurse(arr,0);
// cout<<profit<<endl;
if(profit>0)
return "Alice";
else if(profit==0)
return "Tie";
return "Bob";
}
};