-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproblem_220.js
44 lines (36 loc) Β· 1.16 KB
/
problem_220.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
/**
* Returns the max amount of money you can win with certainty
* @param {number[]} coins
*/
function playCoinGame(coins) {
const coinLength = coins.length;
const game = new Array(coinLength)
.fill(0)
.map(() => Array(coinLength).fill(0));
for (let interval = 0; interval < game.length; interval++) {
for (let i = 0, j = interval; j < game.length; i++, j++) {
// a = game[i + 2, j] - Alice chooses i Bob chooses i + 1
const a = i + 2 <= j ? game[i + 2][j] : 0;
// b = game[i + 1, j - 1] - Alice chooses i , Bob chooses j OR
// Alice chooses j , Bob chooses i
const b = i + 1 <= j - 1 ? game[i + 1][j - 1] : 0;
// c = game[i, j - 2] - Alice chooses j , Bob chooses j - 1
const c = i <= j - 2 ? game[i][j - 2] : 0;
// chose max play
game[i][j] = Math.max(
coins[i] + Math.min(a, b),
coins[j] + Math.min(b, c)
);
}
}
return game[0][game.length - 1];
}
// Game 1
let coins = [6, 9, 1, 2, 16, 8];
console.log(playCoinGame(coins)); // 23
// Game 2
coins = [4, 6, 2, 3];
console.log(playCoinGame(coins)); // 9
// Game 3
coins = [1, 99, 1, 99];
console.log(playCoinGame(coins)); // 198