<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p>

<p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>, <code>9</code>, and <code>16</code> are perfect squares while <code>3</code> and <code>11</code> are not.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 12
<strong>Output:</strong> 3
<strong>Explanation:</strong> 12 = 4 + 4 + 4.
</pre>

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

<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 2
<strong>Explanation:</strong> 13 = 4 + 9.
</pre>

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

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

class Solution:
    def numSquares(self, n: int) -> int:
        """
        My first thoug is a greddy method 

        Explain, 
        1.calculate al perfect squares than are less or equals to n
        2. subtract the bigger squres posible untils the final are 0, because the minimun is 1 so 
            do it is alway posible

        time = sqr(n) , memory = sqr(n)      

        ¿does this give alway the answer?, let´s see, I think that this method alway give 
        a good answer, ¿but,why?, 
        because the way tha I chose the squras alway chose the bigger possible, and this give me
        the lower number of squares required

        but this is not true , I need to try everting so i need to use a DP

        1. Calculate the Square
        2. Calculate the DP
            2.1 : For my dp i going to use a two dimension dp one for the value of the sumatory
                and other for the number of squares

        ________

        I need to make fasther my dp 

        """

        squares = []
        dp = {}
        INF = 100000

        for i in range(1,n+1):
            if i*i>n:
                break
            squares.append(i*i)


        def total_of_squares(sumatory):

            if sumatory == n:
                return 0

            if sumatory > n:
                return INF
            
            if sumatory in dp:
                return  dp[sumatory]

            response = [INF]

            for i in range(len(squares)):
                response.append(total_of_squares(sumatory+squares[i])+1)

            dp[sumatory] = min(response)
            return dp[sumatory]

            
        return total_of_squares(0)
24035