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.
分析:根据动态规划分析得,偶了每一个数都可以看成是一个完美平方+一个普通数。即x=a*a+b。或者这个数本身就是完全平方,例如1,4,9,16等。那么动态规划的公式就
有了:dp[i+j*j]=min(dp[i]+1,dp[i+j*j])
时间复杂度:o(n^2) 空间复杂度:o(n)