解题思路:
动态规划
class Solution {public int numSquares(int n) {int[] dp = new int[n + 1];// 初始化dp数组,默认最坏情况是每个数都是由1相加得到的for (int i = 1; i <= n; i++) {dp[i] = i;}for (int i = 1; i <= n; i++) {for (int j = 1; j * j <= i; j++) {dp[i] = Math.min(dp[i], dp[i - j * j] + 1);}}return dp[n];}
}