-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1140. Stone Game II.cpp
80 lines (77 loc) · 2.61 KB
/
1140. Stone Game II.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class Solution {
public:
// self : have issues
int stoneGameII1(vector<int>& piles) {
int n = piles.size();
vector<vector<int>> dp(n,vector(n,0));
function<int(int,int)> f = [&](int pos,int m){
if(pos>=n) return 0;
if(dp[pos][m]>0) return dp[pos][m];
//dp[pos][m]=0;
if(n-pos <= 2*m) {
for(int i=pos;i<n;++i)
dp[pos][m]+= piles[i];
return dp[pos][m];
}
int sum=0;
int maxAlt=0;
for(int i=pos;i<(pos+2*m);++i){
sum += piles[pos];
int s = f(i+1,max(pos-i+1,m));
if(sum+s> dp[pos][m]){
dp[pos][m] = sum+s;
maxAlt = dp[i+1][max(pos-i+1,m)];
}
//maxAlt = max(maxAlt,dp[i+1][max(pos-i+1,m)]);
}
return maxAlt;
};
f(0,1);
return dp[0][1];
}
//https://leetcode.com/problems/stone-game-ii/discuss/345247/C%2B%2B-DP-(Tabulation)
int stoneGameII2(vector<int>& piles) {
int length = piles.size();
vector<vector<int>>dp(length + 1, vector<int>(length + 1,0));
vector<int> sufsum (length + 1, 0);
for (int i = length - 1; i >= 0; i--) {
sufsum[i] = sufsum[i + 1] + piles[i];
}
for (int i = 0; i <= length; i++) {
dp[i][length] = sufsum[i];
}
for (int i = length - 1; i >= 0; i--) {
for (int j = length - 1; j >= 1; j--) {
for (int X = 1; X <= 2 * j && i + X <= length; X++) {
dp[i][j] = max(dp[i][j], sufsum[i] - dp[i + X][max(j, X)]);
}
}
}
return dp[0][1];
}
// official solution
int stoneGameII(vector<int>& piles) {
int n = piles.size();
vector dp(2, vector(n + 1, vector<int>(n + 1, -1)));
function<int(int, int, int)> f = [&](int p, int i, int m) -> int {
if (i == n) {
return 0;
}
if (dp[p][i][m] != -1) {
return dp[p][i][m];
}
int res = p == 1 ? 1000000 : -1, s = 0;
for (int x = 1; x <= min(2 * m, n - i); x++) {
s += piles[i + x - 1];
if (p == 0) {
res = max(res, s + f(1, i + x, max(m, x)));
}
else {
res = min(res, f(0, i + x, max(m, x)));
}
}
return dp[p][i][m] = res;
};
return f(0, 0, 1);
}
};