279. Perfect Squares

Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.

Example 1:

Input: n = 12
Output: 3 
Explanation: 12 = 4 + 4 + 4.

Example 2:

Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.


class Solution(object):
    def numSquares(self, n):
        """
        :type n: int
        :rtype: int
        """
        dp = [n] *(n+1)
        dp[0] = 0
        
        for i in range(1, n+1):
            j = 1
            while j**2 <= i:
                dp[i] = min(dp[i-j**2]+1, dp[i])
                j +=1
            
            
        return dp[-1]
原文地址:https://www.cnblogs.com/boluo007/p/12542422.html