-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1563-stone-game-v.js
65 lines (54 loc) · 2.09 KB
/
1563-stone-game-v.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* 1563. Stone Game V
* https://leetcode.com/problems/stone-game-v/
* Difficulty: Hard
*
* There are several stones arranged in a row, and each stone has an associated value which
* is an integer given in the array stoneValue.
*
* In each round of the game, Alice divides the row into two non-empty rows (i.e. left row and
* right row), then Bob calculates the value of each row which is the sum of the values of all
* the stones in this row. Bob throws away the row which has the maximum value, and Alice's
* score increases by the value of the remaining row. If the value of the two rows are equal,
* Bob lets Alice decide which row will be thrown away. The next round starts with the remaining
* row.
*
* The game ends when there is only one stone remaining. Alice's is initially zero.
*
* Return the maximum score that Alice can obtain.
*/
/**
* @param {number[]} stoneValue
* @return {number}
*/
var stoneGameV = function(stoneValue) {
const n = stoneValue.length;
const prefixScore = Array.from({ length: n + 1 }, () => 0);
const dp = Array.from({ length: n }, () => new Array(n).fill(-1));
for (let index = 1; index <= n; index++) {
prefixScore[index] = prefixScore[index - 1] + stoneValue[index - 1];
}
return getMaxScore(0, n - 1);
function getMaxScore(left, right) {
if (left >= right) return 0;
if (dp[left][right] !== -1) return dp[left][right];
let result = 0;
for (let index = left; index < right; index++) {
const heap1 = prefixScore[index + 1] - prefixScore[left];
const heap2 = prefixScore[right + 1] - prefixScore[index + 1];
if (heap1 > heap2) {
const score = heap2 + getMaxScore(index + 1, right);
result = Math.max(score, result);
} else if (heap1 < heap2) {
const score = heap1 + getMaxScore(left, index);
result = Math.max(score, result);
} else {
const score1 = heap2 + getMaxScore(index + 1, right);
const score2 = heap1 + getMaxScore(left, index);
result = Math.max(result, score1, score2);
}
}
dp[left][right] = result;
return result;
}
};