I used the idea from this post. But it exceeds time limit. Does anyone have an idea about this?

```
public class Solution {
public int numSquares(int n) {
Queue<Integer> list = new LinkedList<>();
list.add(n);
int count = 0;
while(!list.isEmpty()) {
count++;
int size = list.size();
while(size > 0) {
int cur = list.poll();
for (int i = 1; i * i <= n; i++) {
if (cur == i * i) return count;
if (cur < i * i) break;
list.add(cur - i * i);
}
size--;
}
}
return count;
}
}
```