A few tricks here:

Count down from high roots to small roots using Math.sqrt(int) as a starting point.

Check if the root is evenly divisible with the left over of the number, if so do integer division.

Check if we can do better than the best answer already found before recursing.
public class Solution {
public int squares(int n, int root,int count,int sqmin) { if (count>=sqmin) { return sqmin; } int min=0; for(;root>0;root) { int sqr=(root*root); if (n < sqr) { continue; } else if (n%sqr==0) { min= count+ ((n/sqr)1); } else { min= squares(nsqr,root,count+1,sqmin); } if (min < sqmin) { sqmin=min; } } return sqmin; } public int numSquares(int n) { if (n < 1) return 0; return squares(n,(int)Math.sqrt(n),1,Integer.MAX_VALUE); }
}