当前位置:网站首页>70. Stair Climbing Advanced Edition
70. Stair Climbing Advanced Edition
2022-08-10 00:33:00 【empty__barrel】
Refer to Code Caprice
70. Climbing stairs
This question is a partial modification of the original question:
Let's say you're climbing stairs.You need n steps to get to the top of the building.
Each time you can climb 1 or 2…n steps.How many different ways can you get to the top of a building?
dp array meaning:
dp[i]: There are dp[i] ways to climb to the top of the building with i steps.
Recursive formula:
dp[i] += dp[i -j];
Initialization:
dp[0] must be 1, dp[0] is the basis of all values in the recursion, if dp[0] is 0, other values are 0.
Traversal order:
This is a complete knapsack, so it is a sequential traversal. Through analysis, we can see that the sequence in this dp is an arrangement, so pay attention to the order in which the two for loops are nested
If the number of combinations is sought, the outer for loop traverses the items, and the inner for loops through the backpack
If the number of permutations is sought, the outer for loop traverses the backpack, and the inner for loop traverses the items
Convert the title:
One step, one step, two steps, etc., and then the whole process is, how many steps are taken each time and then to the top of the building.
Think of it this way: the number of steps that can be taken is an array, and the value from the array (each element can be taken repeatedly) is the number of steps required to go up to the top of the building.
At this point, it is clear that this is a complete knapsack problem.
class Solution {public:int climbStairs(int n) {vector<int> dp(n + 1, 0);dp[0] = 1;for (int i = 1; i <= n; i++) { // Traverse the backpackfor (int j = 1; j <= m; j++) { // Traverse itemsif (i - j >= 0) dp[span>i] += dp[i - j];}}return dp[n];}}; The m in the code means that you can climb 1~m steps, and changing m to 2 in the code is the code for this question 70. Climbing stairs can be AC.
边栏推荐
猜你喜欢
随机推荐
kubesphere
Click: 518. Change Exchange II
mysql中的key是怎么用的,或者这个值有什么意义,如下图?
LiveData : Transformations.map and Transformations.switchMap usage
五分钟商学院(基础---商业篇)
上海一科技公司刷单被罚22万,揭露网络刷单灰色产业链
力扣:377. 组合总和 Ⅳ
集群的基础形式
Miscellaneous talk - the sorrow of programmers
34. Fabric2.2 证书目录里各文件作用
如何正则匹配乱码?
68.qt quick-qml多级折叠下拉导航菜单 支持动态添加/卸载 支持qml/widget加载等
金仓数据库 KingbaseGIS 使用手册(6.6. 几何对象校验函数、6.7. 空间参考系函数)
联盟链技术应用的难点
深度学习100例 —— 循环神经网络(RNN)实现股票预测
2021年国内外五大BI厂商——优秀的商业智能工具推荐
【面试高频题】可逐步优化的链表高频题
高数_复习_第4章:向量代数和空间解析几何
[Interface Test] Decoding the request body string of the requests library
Day 12 of learning to program


![[Interface Test] Decoding the request body string of the requests library](/img/99/82ef792dacd398a8a62dd94f235a91.png)






