Skip to content

Commit 4a57681

Browse files
author
hasibulislam999
committed
Stone Gme IV problem solved
1 parent 2c4a7d4 commit 4a57681

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Game Theory/1510_stone-game-iv.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Title: Stone Gme IV
3+
* Description: Initially, there are n stones in a pile. On each player's turn, that player makes a move consisting of removing any non-zero square number of stones in the pile.
4+
* Author: Hasibul Islam
5+
* Date: 06/05/2023
6+
*/
7+
8+
/**
9+
* @param {number} n
10+
* @return {boolean}
11+
*/
12+
var winnerSquareGame = function (n) {
13+
const dp = new Array(n + 1).fill();
14+
return helper(dp, n);
15+
};
16+
17+
function helper(dp, n) {
18+
if (n === 0) return false;
19+
if (dp[n] !== undefined) return dp[n];
20+
21+
for (let i = 1; i <= n; i++) {
22+
const square = i * i;
23+
if (square > n) break;
24+
25+
const next = helper(dp, n - square);
26+
if (!next) return (dp[n] = true);
27+
}
28+
29+
return (dp[n] = false);
30+
}

0 commit comments

Comments
 (0)