-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1140-stone-game-ii.js
51 lines (44 loc) · 1.46 KB
/
1140-stone-game-ii.js
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
/**
* 1140. Stone Game II
* https://leetcode.com/problems/stone-game-ii/
* Difficulty: Medium
*
* Alice and Bob continue their games with piles of stones. There are a number of piles arranged
* in a row, and each pile has a positive integer number of stones piles[i]. The objective of
* the game is to end with the most stones.
*
* Alice and Bob take turns, with Alice starting first.
*
* On each player's turn, that player can take all the stones in the first X remaining piles,
* where 1 <= X <= 2M. Then, we set M = max(M, X). Initially, M = 1.
*
* The game continues until all the stones have been taken.
*
* Assuming Alice and Bob play optimally, return the maximum number of stones Alice can get.
*/
/**
* @param {number[]} piles
* @return {number}
*/
var stoneGameII = function(piles) {
const n = piles.length;
const cache = new Map();
const suffixSums = new Array(n + 1).fill(0);
for (let i = n - 1; i >= 0; i--) {
suffixSums[i] = suffixSums[i + 1] + piles[i];
}
return maxStones(0, 1);
function maxStones(index, m) {
if (index >= n) return 0;
if (2 * m >= n - index) return suffixSums[index];
const key = `${index},${m}`;
if (cache.has(key)) return cache.get(key);
let opponentMin = Infinity;
for (let x = 1; x <= 2 * m; x++) {
opponentMin = Math.min(opponentMin, maxStones(index + x, Math.max(m, x)));
}
const result = suffixSums[index] - opponentMin;
cache.set(key, result);
return result;
}
};