Day45(70, 322, 279)

70. Climbing Stairs

You are climbing a staircase. It takes n steps to reach the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Example 1:

Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.

  1. 1 step + 1 step
  2. 2 steps

Example 2:

Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.

  1. 1 step + 1 step + 1 step
  2. 1 step + 2 steps
  3. 2 steps + 1 step
class Solution {  
    public int climbStairs(int n) {  
        if (n == 1) return 1;  
        int[] stack = new int[2];  
        stack[0] = 1; stack[1] = 2;  
        for (int i = 2; i < n; i++) {  
            int temp = stack[0] + stack[1];  
            stack[0] = stack[1];  
            stack[1] = temp;  
        }  
        return stack[1];  
    }  
}

322. Coin Change

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.

Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

You may assume that you have an infinite number of each kind of coin.

Example 1:

Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1

Example 2:

Input: coins = [2], amount = 3
Output: -1

Example 3:

Input: coins = [1], amount = 0
Output: 0

class Solution {  
    public int coinChange(int[] coins, int amount) {  
        int max = Integer.MAX_VALUE;  
        int[] dp = new int[amount + 1];  
        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. Perfect Squares

Given an integer n, return the least number of perfect square numbers that sum to n.

perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, 149, and 16 are perfect squares while 3 and 11 are not.

Example 1:

Input: n = 12
Output: 3
Explanation: 12 = 4 + 4 + 4.

Example 2:

Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.

class Solution {  
    public int numSquares(int n) {  
        int max = Integer.MAX_VALUE;  
        int[] dp = new int[n + 1];  
        for (int j = 0; j <= n; 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];  
    }  
}

你可能感兴趣的:(leetcode刷题,leetcode,算法,动态规划)