Skip to content

Commit 2362ec7

Browse files
committed
Add solution #1510
1 parent 01c3bcf commit 2362ec7

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,338 LeetCode solutions in JavaScript
1+
# 1,339 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1154,6 +1154,7 @@
11541154
1507|[Reformat Date](./solutions/1507-reformat-date.js)|Easy|
11551155
1508|[Range Sum of Sorted Subarray Sums](./solutions/1508-range-sum-of-sorted-subarray-sums.js)|Medium|
11561156
1509|[Minimum Difference Between Largest and Smallest Value in Three Moves](./solutions/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.js)|Medium|
1157+
1510|[Stone Game IV](./solutions/1510-stone-game-iv.js)|Hard|
11571158
1512|[Number of Good Pairs](./solutions/1512-number-of-good-pairs.js)|Easy|
11581159
1519|[Number of Nodes in the Sub-Tree With the Same Label](./solutions/1519-number-of-nodes-in-the-sub-tree-with-the-same-label.js)|Medium|
11591160
1524|[Number of Sub-arrays With Odd Sum](./solutions/1524-number-of-sub-arrays-with-odd-sum.js)|Medium|

solutions/1510-stone-game-iv.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 1510. Stone Game IV
3+
* https://leetcode.com/problems/stone-game-iv/
4+
* Difficulty: Hard
5+
*
6+
* Alice and Bob take turns playing a game, with Alice starting first.
7+
*
8+
* Initially, there are n stones in a pile. On each player's turn, that player makes a move
9+
* consisting of removing any non-zero square number of stones in the pile.
10+
*
11+
* Also, if a player cannot make a move, he/she loses the game.
12+
*
13+
* Given a positive integer n, return true if and only if Alice wins the game otherwise return
14+
* false, assuming both players play optimally.
15+
*/
16+
17+
/**
18+
* @param {number} n
19+
* @return {boolean}
20+
*/
21+
var winnerSquareGame = function(n) {
22+
const dp = new Array(n + 1).fill(false);
23+
24+
for (let stones = 1; stones <= n; stones++) {
25+
for (let square = 1; square * square <= stones; square++) {
26+
if (!dp[stones - square * square]) {
27+
dp[stones] = true;
28+
break;
29+
}
30+
}
31+
}
32+
33+
return dp[n];
34+
};

0 commit comments

Comments
 (0)