当前位置:网站首页>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.
边栏推荐
- What are the basic steps to develop a quantitative trading strategy?
- 【云原生】一文讲透Kubevela addon如何添加腾讯Crane
- 【微信小程序开发(八)】音频背景音乐播放问题汇总
- LiveData : Transformations.map和 Transformations.switchMap用法
- 高手这样看现货白银走势图
- Mysql/stonedb - slow SQL - 2022-08-09 Q16 analysis
- 深圳堡垒机厂家有哪些?重点推荐哪家?
- Janus Official DEMO Introduction
- UNI-APP_ monitor page scroll h5 monitor page scroll
- 打包报错 AAPT: error: failed to read PNG signature: file does not start with PNG signature.
猜你喜欢
Redis集群
如何正则匹配乱码?
Qt message mechanism and events
迁移学习 & 凯明初始化
安踏携手华为运动健康共同验证冠军跑鞋 创新引领中国体育
shell数组
全球不用交税的国家,为什么不交
Comprehensive analysis of FPGA basics
Transfer Learning & Kemin Initialization
Live Preview | ICML 2022 11 first-author scholars share online neural network, graph learning and other cutting-edge research
随机推荐
Mysql/stonedb - slow SQL - 2022-08-09 Q16 analysis
干涉BGP的选路---社团属性
用户要清晰知道,量化交易并非简单的程序
tiup cluster scale-out
Leetcode 236. 二叉树的最近公共祖先
JS--hashchange事件--使用/教程
tiup cluster stop
What is the stability of the quantitative trading interface system?
使用股票量化交易接口需要具备怎么样的心态
Redis集群
直播预告 | ICML 2022 11位一作学者在线分享神经网络,图学习等前沿研究
数字与中文大写数字互转(5千万亿亿亿亿以上的数字也支持转换)
shell数组
金仓数据库 KingbaseGIS 使用手册(6.5. 几何对象编辑函数)
2022年最新《谷粒学院开发教程》:10 - 前台支付模块
高数_复习_第4章:向量代数和空间解析几何
【微信小程序开发(八)】音频背景音乐播放问题汇总
安踏携手华为运动健康共同验证冠军跑鞋 创新引领中国体育
matplotlib散点图自定义坐标轴(文字坐标轴)
【Burning】It's time to show your true strength!Understand the technical highlights of the 2022 Huawei Developer Competition in one article