```
class Solution {
public:
int ans = INT_MAX;
int numSquares(int n) {
helper(n,0);
return ans;
}
void helper(int n, int nums){
if(n==0){
ans = min(ans, nums);
return;
}
if(nums>=ans) return;
for(int i=int(sqrt(n));i>=1;i--){
int tmp_root = i*i;
int remains = n%(tmp_root);
int curr_nums = n/(tmp_root);
helper(remains, nums+curr_nums);
}
}
};
```