279. 完全平方数


给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。

示例 1:

输入: n = 12
输出: 3 
解释: 12 = 4 + 4 + 4.
示例 2:

输入: n = 13
输出: 2
解释: 13 = 4 + 9.
class Solution {
    public int numSquares(int n) {
        List<Integer> squareList = generateSquareList(n);
        int[] dp = new int[n+1];
        for(int i=1;i<=n;i++){
            int min = Integer.MAX_VALUE;
            for(int square : squareList){
                if(square > i)
                    break;
                min = Math.min(min,dp[i-square]+1);
            }
            dp[i] = min;
        }
        return dp[n];
    }
    private List<Integer> generateSquareList(int n){
        List<Integer> squareList = new ArrayList<>();
        for(int i=1;i*i<=n;i++){
            squareList.add(i*i);
        }
        return squareList;
    }
}

猜你喜欢

转载自blog.csdn.net/xuchonghao/article/details/81011133