当前位置:网站首页>算法-- 爬楼梯(Kotlin)
算法-- 爬楼梯(Kotlin)
2022-07-06 05:18:00 【小米科技Android 研发曹新雨】
题目
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
示例 1:
输入:n = 2
输出:2
解释:有两种方法可以爬到楼顶。
1. 1 阶 + 1 阶
2. 2 阶
示例 2:
输入:n = 3
输出:3
解释:有三种方法可以爬到楼顶。
1 阶 + 1 阶 + 1 阶
1 阶 + 2 阶
2 阶 + 1 阶
提示:
1 <= n <= 45
https://leetcode.cn/problems/climbing-stairs/
解决思路
- 本问题其实常规解法可以分成多个子问题,爬第n阶楼梯的方法数量,等于 2 部分之和
爬上 n-1阶楼梯的方法数量。因为再爬1阶就能到第n阶
爬上 n-2阶楼梯的方法数量,因为再爬2阶就能到第n阶
所以我们得到公式 dp[n] = dp[n-1] + dp[n-2]
同时需要初始化 dp[0]=1 和 dp[1]=1d
时间复杂度:O(n)
2.既然 dp[n] = dp[n-1] + dp[n-2]
那么我们可以用滑动数组去解决
解决方法
fun climbStairs(n: Int): Int {
val intArray = IntArray(n + 1)
intArray[0] = 1
intArray[1] = 1
for (i in 2..n) {
intArray[i] = intArray[i - 1] + intArray[i - 2]
}
return intArray[n]
}
fun climbStairs2(n: Int): Int {
var q = 1
var p = 1
var result = 1
for (i in 2..n) {
result = q + p
q = p
p = result
}
return result
}
总结
1.这个题一下没有想出来怎么做
看了一下题解才做出来
边栏推荐
- 2022 half year summary
- UCF (2022 summer team competition I)
- Nacos - TC Construction of High available seata (02)
- Some common skills on unity inspector are generally used for editor extension or others
- 【OSPF 和 ISIS 在多路访问网络中对掩码的要求】
- Unity gets the width and height of Sprite
- Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
- Sliding window problem review
- Rce code and Command Execution Vulnerability
- nacos-高可用seata之TC搭建(02)
猜你喜欢

Pagoda configuration mongodb

Codeforces Round #804 (Div. 2)

Leetcode dynamic planning day 16

02. 开发博客项目之数据存储
![[lgr-109] Luogu may race II & windy round 6](/img/fe/d5b67c7dff759c519a04da023630ea.png)
[lgr-109] Luogu may race II & windy round 6

Cuda11.1 online installation

RT thread analysis - object container implementation and function

【LeetCode】18、四数之和

nacos-高可用seata之TC搭建(02)

趋势前沿 | 达摩院语音 AI 最新技术大全
随机推荐
Idea one key guide package
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
2021 robocom world robot developer competition - undergraduate group (semi-finals)
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
浅谈镜头滤镜的类型及作用
Set detailed map + interview questions
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
Principle and performance analysis of lepton lossless compression
关于Unity Inspector上的一些常用技巧,一般用于编辑器扩展或者其他
Collection + interview questions
Huawei equipment is configured with OSPF and BFD linkage
Compilation and connection of shader in games202 webgl (learn from)
Configuration file converted from Excel to Lua
Implementing fuzzy query with dataframe
EditorUtility. The role and application of setdirty in untiy
Oracle query table index, unique constraint, field
Vulhub vulnerability recurrence 69_ Tiki Wiki
Golang -- TCP implements concurrency (server and client)
Ad20 is set with through-hole direct connection copper sheet, and the bonding pad is cross connected
Promotion hung up! The leader said it wasn't my poor skills