-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
43 lines (40 loc) · 993 Bytes
/
main.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
// URL: https://leetcode.com/problems/stone-game-iii/
let notComputed = 1000000000;
let dp = [];
function f(stoneValue = [], n, i) {
if (i == n) {
return 0;
}
if (dp[i] != notComputed) {
return dp[i];
}
dp[i] = stoneValue[i] - f(stoneValue, n, i + 1);
if (i + 2 <= n) {
dp[i] = Math.max(dp[i], stoneValue[i]
+ stoneValue[i + 1] - f(stoneValue, n, i + 2));
}
if (i + 3 <= n) {
dp[i] = Math.max(dp[i], stoneValue[i] + stoneValue[i + 1]
+ stoneValue[i + 2] - f(stoneValue, n, i + 3));
}
return dp[i];
}
/**
* @param {number[]} stoneValue
* @return {string}
*/
function stoneGameIII(stoneValue = []) {
let n = stoneValue.length;
dp = new Array(n + 1);
for (let i = 0; i < n; i++) {
dp[i] = notComputed;
}
let dif = f(stoneValue, stoneValue.length, 0);
if (dif > 0) {
return "Alice";
}
if (dif < 0) {
return "Bob";
}
return "Tie";
}