https://leetcode.com/problems/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 {
public:
int numSquares(int n) {
int* dp = new int[n+1];
dp[0] = 0;
for(int i = 1; i <= n; ++i){
int tem = INT_MAX;
for(int j = 1; j*j <= i; ++j){
tem = min(tem, dp[i-j*j]+1);
}
dp[i] = tem;
}
return dp[n];
}
};
优化版的动态规划:
将vector设置为了static变量(我也不知道这个修饰符具体是个什么作用),注意使用这个修饰符必须确保vector不能一开始就有全部的n+1个空间
class Solution {
public:
int numSquares(int n) {
static vector<int> dp({0}); //static修饰符把时间从100ms降到了8ms
// dp[0] = 0;
while(dp.size() <= n){
int tem = INT_MAX;
int m = dp.size();
for(int j = 1; j*j <= m; ++j){
tem = min(tem, dp[m-j*j]+1);
}
dp.push_back(tem);
}
return dp[n];
}
};
数论方法:
参考:
https://www.jianshu.com/p/2925f4d7511b
https://blog.csdn.net/qq_34202873/article/details/79297033
class Solution {
public:
int numSquares(int n) {
while (n % 4 == 0) n /= 4;
if (n % 8 == 7) return 4;
for (int a = 0; a * a <= n; ++a) {
int b = sqrt(n - a * a);
if (a * a + b * b == n) {
return !!a + !!b; //两个感叹号起到把整型变为布尔型的作用
}
}
return 3;
}
};
还有一些稀奇古怪难以理解的方法,罗列如下:
一、上述参考中第二篇里的BFS
class Solution
{
public:
int numSquares(int n)
{
if (n <= 0)
{
return 0;
}
// perfectSquares contain all perfect square numbers which
// are smaller than or equal to n.
vector<int> perfectSquares;
// cntPerfectSquares[i - 1] = the least number of perfect
// square numbers which sum to i.
vector<int> cntPerfectSquares(n);
// Get all the perfect square numbers which are smaller than
// or equal to n.
for (int i = 1; i*i <= n; i++)
{
perfectSquares.push_back(i*i);
cntPerfectSquares[i*i - 1] = 1;
}
// If n is a perfect square number, return 1 immediately.
if (perfectSquares.back() == n)
{
return 1;
}
// Consider a graph which consists of number 0, 1,...,n as
// its nodes. Node j is connected to node i via an edge if
// and only if either j = i + (a perfect square number) or
// i = j + (a perfect square number). Starting from node 0,
// do the breadth-first search. If we reach node n at step
// m, then the least number of perfect square numbers which
// sum to n is m. Here since we have already obtained the
// perfect square numbers, we have actually finished the
// search at step 1.
queue<int> searchQ;
for (auto& i : perfectSquares)
{
searchQ.push(i);
}
int currCntPerfectSquares = 1;
while (!searchQ.empty())
{
currCntPerfectSquares++;
int searchQSize = searchQ.size();
for (int i = 0; i < searchQSize; i++)
{
int tmp = searchQ.front();
// Check the neighbors of node tmp which are the sum
// of tmp and a perfect square number.
for (auto& j : perfectSquares)
{
if (tmp + j == n)
{
// We have reached node n.
return currCntPerfectSquares;
}
else if ((tmp + j < n) && (cntPerfectSquares[tmp + j - 1] == 0))
{
// If cntPerfectSquares[tmp + j - 1] > 0, this is not
// the first time that we visit this node and we should
// skip the node (tmp + j).
cntPerfectSquares[tmp + j - 1] = currCntPerfectSquares;
searchQ.push(tmp + j);
}
else if (tmp + j > n)
{
// We don't need to consider the nodes which are greater ]
// than n.
break;
}
}
searchQ.pop();
}
}
return 0;
}
};
二、LeetCode中12ms的样例
class Solution {
int ns_helper(int n, int k,int max, vector<int>& rec) {
//if(ps.count(n)>0) return 1;
if(rec[n]!=0) return rec[n];
if(max ==1) return 1;
int min_v =max;
for(int x=k;x>=1;x--) {
if(x*x>n) {
continue;
}
if(x*x==n) {
min_v =1;
break;
};
//从这里开始不知道他在干什么
if(min_v * (x*x)<n) break;
// cout<< n-*x<<endl;
int rv = 1+ ns_helper(n-x*x, k, min_v -1,rec);
min_v = min(rv,min_v);
if (min_v == 2) {
break;
}
}
rec[n] = min_v;
return min_v;
}
public:
int numSquares(int n) {
/*vector<int>numsq(n+1,0);
int k = sqrt(n);
numsq[0] =0;
numsq[1] = 1;
for(int i=2;i<=n;i++) {
numsq[i]=i;
for(int j =k;j>1;j--) {
if(j*j>i) continue;
if(j*j==i) {
numsq[i] =1;
break;
}
numsq[i] =min(numsq[i], 1+numsq[i-j*j]);
if(numsq[i]==2) break;
}
}
return numsq[n];
} */
vector<int> rec(n+1,0);
int k = sqrt(n);
/* set<int> ps;
for(int i=1;i<=k;i++) {
ps.insert(i*i);
}*/
return ns_helper(n,k,n,rec);
}
};