-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstoneGameII.cpp
More file actions
27 lines (25 loc) · 860 Bytes
/
stoneGameII.cpp
File metadata and controls
27 lines (25 loc) · 860 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
// Source: https://leetcode.com/problems/stone-game-ii/
// Author: Miao Zhang
// Date: 2021-04-12
class Solution {
public:
int stoneGameII(vector<int>& piles) {
const int n = piles.size();
unordered_map<int, int> cache;
function<int(int, int)> solve = [&](int s, int M) {
if (s >= n) return 0;
const int key = (s << 8) | M;
if (cache.count(key)) return cache[key];
int best = INT_MIN;
int curr = 0;
for (int x = 1; x <= 2 * M; ++x) {
if (s + x > n) break;
curr += piles[s + x - 1];
best = max(best, curr - solve(s + x, max(x, M)));
}
return cache[key] = best;
};
int total = accumulate(begin(piles), end(piles), 0);
return (total + solve(0, 1)) / 2;
}
};