题目描述:
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.
求一个数最少由多少个平方数相加得到,显然是利用动态规划,对于一个数i,遍历所有的j,满足j*j<=i,那么dp[i]=min(dp[i-j*j]+1,dp[i])。
class Solution {
public:
int numSquares(int n) {
if(n==0) return 0;
vector<int> dp(n+1,INT_MAX);
dp[0]=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j*j<=i;j++)
{
dp[i]=min(dp[i-j*j]+1,dp[i]);
}
}
return dp[n];
}
};