forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
stone-game-ii.cpp
30 lines (28 loc) · 886 Bytes
/
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
// Time: O(n*(logn)^2)
// Space: O(nlogn)
class Solution {
public:
int stoneGameII(vector<int>& piles) {
for (int i = piles.size() - 2; i >= 0; --i) {
piles[i] += piles[i + 1];
}
unordered_map<int, unordered_map<int, int>> lookup;
return dp(piles, &lookup, 0, 1);
}
private:
int dp(const vector<int>& piles,
unordered_map<int, unordered_map<int, int>> *lookup,
int i, int m) {
if (i + 2 * m >= piles.size()) {
return piles[i];
}
if (!lookup->count(i) || !(*lookup)[i].count(m)) {
int min_dp = numeric_limits<int>::max();
for (int x = 1; x <= 2 * m; ++x) {
min_dp = min(min_dp, dp(piles, lookup, i + x, max(m, x)));
}
(*lookup)[i][m] = piles[i] - min_dp;
}
return (*lookup)[i][m];
}
};