当前位置:网站首页>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.
边栏推荐
猜你喜欢

shell array

新增一地公布2022下半年软考报考时间

32 JZOF 】 【 print down on binary tree

Install win7 virtual machine in Vmware and related simple knowledge

6款跨境电商常用工具汇总

HUAWEI CLOUD escorts the whole process of "Wandering Ark" for the first time, creating a popular brand

2022-08-09 mysql/stonedb-慢SQL-Q16分析

测试2年,当时身边一起入行的朋友已经月薪20k了,自己还没过万,到底差在了哪里?

杭电多校-Counting Stickmen-(思维+组合数+容斥)

集群的基础形式
随机推荐
Gold Warehouse Database KingbaseGIS User Manual (6.2. Management Functions)
What is the stability of the quantitative trading interface system?
联盟链技术应用的难点
34. Fabric2.2 证书目录里各文件作用
力扣:279.完全平方数
linux上使用docker安装redis
String类常用方法
ElasticSearcch集群
金仓数据库 KingbaseGIS 使用手册(6.3. 几何对象创建函数)
金仓数据库 KingbaseGIS 使用手册(6.4. 几何对象存取函数)
LiveData : Transformations.map and Transformations.switchMap usage
杭电多校-Counting Stickmen-(思维+组合数+容斥)
金仓数据库 KingbaseGIS 使用手册(6.2. 管理函数)
32 JZOF 】 【 print down on binary tree
安踏携手华为运动健康共同验证冠军跑鞋 创新引领中国体育
高数_复习_第4章:向量代数和空间解析几何
Janus Official DEMO Introduction
探索TiDB Lightning源码来解决发现的bug
matplotlib散点图颜色分组图例
Basic operations of xlrd and xlsxwriter