LeetCode刷题 Day45

68 阅读3分钟

LeetCode刷题 Day45

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.

步骤:

  1. dp index 和 value的意义: index为背包容量, value为达成square number所需要的个数
  2. 递推公式: dp[i] = Math.min(dp[i - num] + 1, dp[i]);
  3. 初始化: dp[0] = 0, dp[1], dp[2]... 为 MaxValue
  4. 遍历顺序: 因为是完全背包 且不计算排列组合问题 所以内外循环顺序无影响,顺序遍历
  5. 举例论证:

image.png

代码:

var numSquares = function(n) {
    let dp = Array(n + 1).fill(Number.MAX_VALUE);
    dp[0] = 0;
    let item = Math.ceil(Math.sqrt(n));

    for (let i = 0; i <= n; i++) {
        for (let j = 0; j <= item; j++) {
            if (i >= j * j) dp[i] = Math.min(dp[i - j * j] + 1, dp[i]);
        }
    }
    return dp[n];
};

时间复杂度: O(mn) 空间复杂度: O(n)


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
  1. dp index 和 value的意义: index为背包容量, value为达成square number所需要的个数
  2. 递推公式: dp[i] = Math.min(dp[i - coin] + 1, dp[i]);
  3. 初始化: dp[0] = 0, dp[1], dp[2]... 为 MaxValue
  4. 遍历顺序: 因为是完全背包 且不计算排列组合问题 所以内外循环顺序无影响,顺序遍历
  5. 举例论证:

image.png

var coinChange = function(coins, amount) {
    let dp = Array(amount + 1).fill(Number.MAX_VALUE);

    dp[0] = 0;

    for (let i = 0; i <= amount; i++) {
        for (let coin of coins) {
            if (coin <= i) {
                dp[i] = Math.min(dp[i - coin] + 1, dp[i]);
            }
        }
    }
    if (dp[amount] == Number.MAX_VALUE) return -1;
    return dp[amount]
};

时间复杂度: O(mn) 空间复杂度: O(n)


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

步骤:

  1. dp index 和 value的意义: index为背包容量, value达到背包容量后不同步数种类
  2. 递推公式: dp[i] += dp[i - step];
  3. 初始化: dp[0] = 1, 为1时基础。 dp[1], dp[2]... 为 0。
  4. 遍历顺序: 因为是完全背包 且计算排列 所以内外循环顺序有影响,背包在外,步数在内, 顺序遍历

代码:

var climbStairs = function(n) {
    let dp = Array(n + 1).fill(0);
    dp[0] = 1;
    let steps = [1, 2];
    for (let i = 0; i <= n; i++) {
        for (let step of steps) {
            if (step <= i) dp[i] += dp[i - step];
        }
    }

    return dp[n];
};

时间复杂度: O(2 * n), 空间复杂度: O(n)