I code it with C++. The Code is almost the same with the code in coins change. But that code is accepted, this is not.Thanks a lot for helping solving this problem!!

```
class Solution {
public:
int numSquares(int n) {
if(n==0)return 0;
while(!pq1.empty())pq1.pop();
while(!pq1.empty())pq2.pop();
int *visit = new int[n];
for(int i=1;i<=int(sqrt(n));i++)
{
pq1.push(n-i*i);
visit[n-i*i]=1;
//cout<<n-i*i<<endl;
}
for(int count=1;count<=n;count++){
queue<int> &pq3 = (count%2!=0)?pq1:pq2;
queue<int> &pq4 = (count%2!=0)?pq2:pq1;
while(!pq3.empty()){
int temp = pq3.front();
pq3.pop();
if(temp==0)return count;
for(int k=1;k<=int(sqrt(temp));k++){
if(temp-k*k<0)continue;
if(visit[temp-k*k]==1)continue;
pq4.push(temp-k*k);
visit[temp-k*k]=1;
}
}
}
delete[]visit;
return n;
}
private:
queue<int> pq1;
queue<int> pq2;
};
```