Journey of LeetCode|DAY 39
Preface
This is a new day to continue my Dynamic Programming journey.
Learn something new and keep reviewing what I learnt before.
1. Climbing Stairs
LeetCode Link: 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 step + 1 step
- 2 steps
Example 2:
Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
- 1 step + 1 step + 1 step
- 1 step + 2 steps
- 2 steps + 1 step
Constraints:
1 <= n <= 45
Analysis and Solution
Dynamic Programming
LeetCode C++ as followings Dynamic Programming
class Solution {
public:
int climbStairs(int n) {
vector<int> dp(n + 1, 0);
dp[0] = 1;
for (int i = 1; i <= n; i++) { // traverse backpack
for (int j = 1; j <= 2; j++) { // traverse items; the order cannot be reversed
if (i - j >= 0) dp[i] += dp[i - j];
}
}
return dp[n];
}
};
2. Coin Change
LeetCode Link: 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
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104
Analysis and Solution
Dynamic Programming
LeetCode C++ as followings Dynamic Programming
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> dp(amount + 1, INT_MAX);
dp[0] = 0;
for (int i = 0; i < coins.size(); i++) { // traverse items
for (int j = coins[i]; j <= amount; j++) { // traverse backpack
if (dp[j - coins[i]] != INT_MAX) { // skip if dp[j - coins[i]] is default value
dp[j] = min(dp[j - coins[i]] + 1, dp[j]);
}
}
}
if (dp[amount] == INT_MAX) return -1;
return dp[amount];
}
};
3. Perfect Squares
LeetCode Link: 279. Perfect Squares
Given an integer n, return the least number of perfect square numbers that sum to n.
A 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, 1, 4, 9, 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.
Constraints:
1 <= n <= 10^4
Analysis and Solution
Dynamic Programming
LeetCode C++ as followings Dynamic Programming
class Solution {
public:
int numSquares(int n) {
vector<int> dp(n + 1, INT_MAX);
dp[0] = 0;
for (int i = 0; i <= n; i++) { // traverse backpack
for (int j = 1; j * j <= i; j++) { // traverse items
dp[i] = min(dp[i - j * j] + 1, dp[i]);
}
}
return dp[n];
}
};