class Solution { public int numSquares(int n) { int[] dp = new int[n + 1]; dp[1] = 1; for (int i = 2; i <= n; i++) { int t = (int) Math.sqrt(i); if (t * t == i) {//square number dp[i] = 1; continue; } int min = Integer.MAX_VALUE; int jj = 0; for (int j = t; j >= 1; j--) { jj = j * j; min = Math.min(min, dp[jj] + dp[i - jj]); } dp[i] += min; } return dp[n]; }}