```
public class Solution {
public int minRes;
public int numSquares(int n) {
minRes = Integer.MAX_VALUE;
find(n, 0);
return minRes;
}
public void find(int n, int count){
if(n < 4){
count += n;
if(count < minRes){
minRes = count;
}
return;
}
int newCount = count + 1;
if(newCount > minRes){
return;
}
int maxsqrt = (int)Math.sqrt(n);
if(maxsqrt*maxsqrt == n){
if(newCount < minRes){
minRes = newCount;
}
return;
}
int i = maxsqrt;
int mid = (maxsqrt + 2)/2;
while(i>= mid){
find(n - i*i, newCount);
i--;
}
}
```

}