publicintnumSquares(int n) {if (n <2) {return1; }// find all the squres smaller than nArrayList<Integer> squares =newArrayList<>();int s =1;while (s * s <= n) {squares.add(s * s); s++; }// backpack, item = all the squares, value = 0 to nint[][] dp =newint[squares.size() +1][n +1];// init first row to MAX, because you need to put infinite among of 0 item in to fill the bagfor (int i =0; i <= n; i++) { dp[0][i] =Integer.MAX_VALUE; }for (int i =1; i <=squares.size(); i++) {for (int j =1; j <= n; j++) { dp[i][j] = dp[i -1][j];if (j >=squares.get(i -1)) { dp[i][j] =Math.min(dp[i][j], dp[i][j -squares.get(i -1)] +1); } } }return dp[squares.size()][n];}