<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&#39;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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation: </strong>Alice can remove 1 stone winning the game because Bob doesn&#39;t have any moves.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> false
<strong>Explanation: </strong>Alice can only remove 1 stone, after that Bob removes the last one winning the game (2 -&gt; 1 -&gt; 0).
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> true
<strong>Explanation:</strong> n is already a perfect square, Alice can win with one move, removing 4 stones (4 -&gt; 0).
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    def winnerSquareGame(self, n: int) -> bool:
        # 1. My Approach: Game Theory + Recursion + Memo
        # each player wants the worst for its opponent
        memo = dict()   # key:(i stones, Player), val: Alice wins  

        def subProb(n, player):
            """
            player = 1 for Alice, player = 0 for Bob
            return if Alice can win the game
            """
            if n == 1:
                if player:
                    return True
                else:
                    return False
            # if n is a square number, current player wins
            if int(sqrt(n)) ** 2 == n:
                if player:
                    return True
                else:
                    return False
            if (n, player) in memo:
                return memo[(n, player)]

            # try all choices: all square numbers below n    
            alice_win_choice = None
            alice_loose_choice = None
            for root in range(1, int(sqrt(n)) + 1):
                choice = root ** 2
                res = subProb(n - choice, 1 - player)
                if player:      # curr is Alice, wants Alice to win
                    if res:
                        alice_win_choice = choice
                        break       # found desirable ans, stop trying
                if not player:
                    if not res:
                        alice_loose_choice = choice
                        break
           
            if player:
                if not alice_win_choice:
                    memo[(n, player)] = False
                else:
                    memo[(n, player)] = True
            else:
                if not alice_loose_choice:
                    memo[(n, player)]= True
                else:
                    memo[(n, player)] = False
           
            return memo[(n, player)]
       
        return subProb(n, 1)
66663