代码随想录算法训练营day45 70.爬楼梯(进阶) 322.零钱兑换 279.完全平方数

题目链接70.爬楼梯(进阶)

class Solution {
    public int climbStairs(int n) {
        int[] dp = new int[n+1];
        int m = 2;
        dp[0] = 1;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if(i >= j)
            dp[i] += dp[i-j];
            }
        }
        return dp[n];
    }
}

题目链接322.零钱兑换

class Solution {
    public int coinChange(int[] coins, int amount) {
        int[] dp = new int[amount+1];
        int max = Integer.MAX_VALUE;
        for(int j = 0; j < dp.length; j++){
            dp[j] = max;
        }
        dp[0] = 0;
        for(int i = 0; i < coins.length; i++){
            for(int j = coins[i]; j <= amount; j++){
                if(dp[j-coins[i]] != max){
                    dp[j] = Math.min(dp[j], dp[j-coins[i]] + 1);
                }
            }
        }
        return dp[amount] == max ? -1:dp[amount];
    }
}

题目链接279.完全平方数

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

你可能感兴趣的:(算法)