class Solution { private int w[]; private int dp[]; public int numSquares(int n) { w=new int[(int) (Math.sqrt(n)+1)]; dp=new int[n+1]; for(int i=1;i<=Math.sqrt(n);++i) { w[i]=i*i; } for(int i=1;i<w.length;++i) { for(int j=w[i];j<=n;++j) { if(j==w[i]) { dp[j]=1; }else { if(dp[j-w[i]]!=0) { if(dp[j]==0) { dp[j]=dp[j-w[i]]+1; }else { dp[j]=Math.min(dp[j], dp[j-w[i]]+1); } } } } } return dp[n]; } }