1249. Stone Game IV

HardDynamic ProgrammingGame Theory

A pile holds n stones. Alice and Bob alternate turns, starting with Alice, and each turn a player removes a non-zero square number of stones. A player who cannot move loses. Assuming optimal play, return true if Alice wins. The input is JSON {n}.

Input: JSON {n}.

Output: Boolean — true if Alice wins.

Examples

Example 1
Input: {"n":1}
Output: true
Explanation: Alice takes the single stone.
Example 2
Input: {"n":2}
Output: false
Explanation: Alice must take 1, leaving Bob a win.
Example 3
Input: {"n":4}
Output: true
Explanation: Alice takes all four.

Constraints

Asked by

InfosysMicrosoftBloombergGoogle
Solve this problem in the editor →