<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>

// Memoization
class Solution {
    public:
    static vector<int> power;
    Solution()
    {
        int n=100001;
        int i=1;
        int lastSquare=1;
        while((i*i)<=n)
        {
            power.push_back(i*i);
            i++;
        }
    }
    
    int help(int n,int turn,vector<vector<int>>& memo)
    {
        // base case
        if(n<=0)
        return !turn;
    
        // memo check
        if(memo[n][turn]!=-1)
        return memo[n][turn];
        
        // if it is a prefect square
        int a = floor((double)sqrt(n));
        int b = ceil((double)sqrt(n));
        
        if(a==b)
        return memo[n][turn]=int(turn);
        
        // recursive calls
        if(turn)
        {
            int i=0;
            while(power[i]<=n)
            {
                int ans=help(n-power[i],!turn,memo);
                if(ans)
                return memo[n][turn]=1;
                
                i++;
            }
            return memo[n][turn]=0;
            
        }
        else
        {
            int i=0;
            while(power[i]<=n)
            {
                int ans=help(n-power[i],!turn,memo);
                if(!ans)
                return memo[n][turn]=0;
                
                i++;
            }
            return memo[n][turn]=1;
        }
    }
    
    bool winnerSquareGame(int n) {
        vector<vector<int>> memo(n+1,vector<int>(2,-1));
        int ans=help(n,1,memo);
        return ans;
//         bool turn=1;
//         while(n>=1)
//         {
//             if(turn)
//             {
//                 if(power[n]==n)
//                 return 1;
                
//                 n-=power[n];
//                 turn=(!turn);
//             }
//             else
//             {
//                 if(power[n]==n)
//                 return 0;
                
//                 n-=power[n];
//                 turn=(!turn);
//             }
//         }
//         return 0;
    }
};

vector<int> Solution::power;


// n=8
// alice - 4, 

// n=4
// bob - 
30328