当前位置:网站首页>Leetcode70 (Advanced), 322
Leetcode70 (Advanced), 322
2022-07-05 00:23:00 【The left wheel of fate】
List of articles
70. climb stairs ( Advanced )
Suppose you're climbing the stairs . need n You can reach the top of the building .
Every time you climb 1 or 2 A stair . How many different ways can you climb to the top of the building ?
Example 1:
Input :n = 2
Output :2
explain : There are two ways to climb to the top .
1. 1 rank + 1 rank
2. 2 rank
Example 2:
Input :n = 3
Output :3
explain : There are three ways to climb to the top .
1. 1 rank + 1 rank + 1 rank
2. 1 rank + 2 rank
3. 2 rank + 1 rank
Tips :
1 <= n <= 45
analysis
- Advanced : First use the solution of complete knapsack , And then expand to every walk 1,2,3…m A ladder ( The title is 1,2 individual ), That is, the template for finding a general solution
- When we did this problem before , Through inductive analysis , Using recurrence formula , Dynamic programming yields a Fibonacci sequence . See the link for details leetcode70,746
- Now let's look at the topic , In fact, I'm asking how many ways I can climb to the n Steps ( That is, how many ways to fill your backpack ), It could be a combination , It may also be an arrangement , We need to give an example to verify .
- For example, the third step , It can be for (2,1) It can also be for (1,2), These two ways are different , So make sure this is a
The problem of permutation
- dp[j] The capacity is j My backpack is full of dp[j] Ways of planting
- The traversal order is double positive loop : Backpack first , Post item
- Array initialization
dp[0] = 1
- If you change it, you can 3,4,5…m steps , Just modify the last range of the item line .
- Actually , The above problems can also be abstracted into subset summation problems , That is, items are a collection , The number of steps is the target and .( And leetcode577 similar )leetcode518,377
Code ( Dynamic programming )
class Solution:
def climbStairs(self, n: int) -> int:
dp = [0]*(n+1)
dp[0] = 1
for i in range(1,n+1): # Backpack first
for j in range(1,3): # Post item ( Only one walk 、 Two steps )
if i >= j: # If the backpack mass is greater than or equal to the item
dp[i] += dp[i-j]
return dp[n]
Through screenshots
322. Change for
Give you an array of integers coins , Coins of different denominations ; And an integer amount , Represents the total amount .
Calculate and return the amount needed to make up the total amount The minimum number of coins . If no combination of coins can make up the total amount , return -1 .
You can think of the number of coins of each kind as infinite .
Example 1:
Input :coins = [1, 2, 5], amount = 11
Output :3
explain :11 = 5 + 5 + 1
Example 2:
Input :coins = [2], amount = 3
Output :-1
Example 3:
Input :coins = [1], amount = 0
Output :0
Tips :
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104
analysis ( Dynamic programming )
- For the first time in dynamic programming , Find the least number of questions , What we think of is the problem of finding the maximum number before .
- First , Coin infinity , Abstract into a complete knapsack problem .
- dp[j] Express : The total amount is j The minimum number of coins required is dp[j]
- Double forward traversal , Outer objects , Inner backpack , The starting point of the backpack , Start with the weight of the item , That is, the backpack can hold the item
- Compare the minimum , First of all, affirm your participation , Secondly, there is the minimum value of the previous state +1( Add one more number to meet the backpack capacity ), namely
dp[j] = min(dp[j],dp[j-nums[i]]+1)
, initial value dp[0] = 0, Be sure to set , Otherwise, there will be problems in the comparison process . - Because the comparison is the minimum , So I initialize it all as backpack space +1( Equivalent to the maximum value of this question ), As before, find the maximum , We initialize to 0 equally . avoid max,min Cause abnormal .
- therefore , Finally, if the value I asked for is equal to the value I set, it means that there is no result , return -1
Code
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
# dp[j]: Capacity of j The minimum number of coins used in your backpack is dp[j]
dp = [amount+1]*(amount+1)
dp[0] = 0
for i in range(len(coins)): # Traverse the items
for j in range(coins[i],amount+1): # Traverse the backpack
dp[j] = min(dp[j],dp[j-coins[i]]+1)
if dp[amount] == amount+1:
return -1
return dp[amount]
Through screenshots
If there is a mistake , Please correct me , Welcome to exchange , thank you *(・ω・)ノ
边栏推荐
- GDB常用命令
- [paper reading] Tun det: a novel network for meridian ultra sound nodule detection
- Summary of week 22-07-02
- Deux nombres se remplacent
- Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
- Using fast parsing intranet penetration to realize zero cost self built website
- 青海省国家湿地公园功能区划数数据、全国湿地沼泽分布数据、全国省市县自然保护区
- Consolidated expression C case simple variable operation
- Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
- uniapp微信小程序拿来即用的瀑布流布局demo2(方法二)(复制粘贴即可使用,无需做其他处理)
猜你喜欢
Using fast parsing intranet penetration to realize zero cost self built website
XML的解析
Get to know ROS for the first time
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
Face recognition 5- insight face padding code practice notes
URLs and URIs
Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
Acrel-EMS综合能效平台在校园建设的意义
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
【C】 (written examination questions) pointer and array, pointer
随机推荐
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
AcWing164. 可达性统计(拓扑排序+bitset)
「运维有小邓」域密码策略强化器
微服务(Microservice)那点事儿
业务实现-日志写到同一个行数据里面
Huawei employs data management experts with an annual salary of 2million! The 100 billion market behind it deserves attention
22-07-02周总结
电力运维云平台:开启电力系统“无人值班、少人值守”新模式
P3304 [sdoi2013] diameter (diameter of tree)
JS 将伪数组转换成数组
PMP certificate renewal process
How to avoid arc generation—— Aafd fault arc detector solves the problem for you
P4408 [NOI2003] 逃学的小孩(树的直径)
uniapp微信小程序拿来即用的瀑布流布局demo2(方法二)(复制粘贴即可使用,无需做其他处理)
Life is changeable, and the large intestine covers the small intestine. This time, I can really go home to see my daughter-in-law...
Illustrated network: what is gateway load balancing protocol GLBP?
如何有效对直流列头柜进行监测
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
Application of multi loop instrument in base station "switching to direct"
使用快解析搭建自己的minecraft服务器