Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...
) which sum to n.
For example, given n = 12
, return 3
because 12 = 4 + 4 + 4
; given n = 13
, return 2
because 13 = 4 + 9
.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
Show Similar Problems
参考discusshttps://leetcode.com/discuss/58056/summary-of-different-solutions-bfs-static-and-mathematics
class Solution {
public:
int numSquares(int n) {
if(n<=0)
return 0;
// cntPerfectSquares[i] = the least number of perfect square numbers
// which sum to i. Note that cntPerfectSquares[0] is 0.
vector<int> cntPerfectSqures(n+1,INT_MAX);
cntPerfectSqures[0] = 0;
for(int i = 1;i<n+1;i++)
{
// For each i, it must be the sum of some number (i - j*j) and
// a perfect square number (j*j).
for(int j = 1;j*j<=i;j++)
{
cntPerfectSqures[i] = min(cntPerfectSqures[i],cntPerfectSqures[i-j*j]+1);
}
}
return cntPerfectSqures.back();
}
};