Class Solution

java.lang.Object
g1501_1600.s1510_stone_game_iv.Solution

public class Solution extends Object
1510 - Stone Game IV.<p>Hard</p> <p>Alice and Bob take turns playing a game, with Alice starting first.</p> <p>Initially, there are <code>n</code> stones in a pile. On each player&rsquo;s turn, that player makes a <em>move</em> consisting of removing <strong>any</strong> non-zero <strong>square number</strong> of stones in the pile.</p> <p>Also, if a player cannot make a move, he/she loses the game.</p> <p>Given a positive integer <code>n</code>, return <code>true</code> if and only if Alice wins the game otherwise return <code>false</code>, assuming both players play optimally.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 1</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> Alice can remove 1 stone winning the game because Bob doesn&rsquo;t have any moves.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 2</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> Alice can only remove 1 stone, after that Bob removes the last one winning the game (2 -> 1 -> 0).</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> n = 4</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> n is already a perfect square, Alice can win with one move, removing 4 stones (4 -> 0).</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 10<sup>5</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • winnerSquareGame

      public boolean winnerSquareGame(int n)