当前位置:网站首页>Algorithm -- climbing stairs (kotlin)
Algorithm -- climbing stairs (kotlin)
2022-07-06 05:22:00 【Xiaomi technology Android research and development caoxinyu】
subject
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 rank + 1 rank + 1 rank
1 rank + 2 rank
2 rank + 1 rank
Tips :
1 <= n <= 45
https://leetcode.cn/problems/climbing-stairs/
Solutions
- In fact, the conventional solution of this problem can be divided into several subproblems , Climb the first n The number of ways to step stairs , be equal to 2 The sum of parts
Climb up n-1 The number of ways to step stairs . Because climb again 1 You can get to the third level n rank
Climb up n-2 The number of ways to step stairs , Because climb again 2 You can get to the third level n rank
So we get the formula dp[n] = dp[n-1] + dp[n-2]
At the same time, you need to initialize dp[0]=1 and dp[1]=1d
Time complexity :O(n)
2. since dp[n] = dp[n-1] + dp[n-2]
Then we can use sliding array to solve it
resolvent
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
}
summary
1. I didn't figure out how to do this problem
I read the solution of the problem before I worked it out
边栏推荐
- Ad20 is set with through-hole direct connection copper sheet, and the bonding pad is cross connected
- 【华为机试真题详解】统计射击比赛成绩
- 【torch】|torch.nn.utils.clip_grad_norm_
- pix2pix:使用条件对抗网络的图像到图像转换
- Promotion hung up! The leader said it wasn't my poor skills
- Cuda11.1 online installation
- What are the advantages of the industry private network over the public network? What specific requirements can be met?
- 【LeetCode】18、四数之和
- SQLite queries the maximum value and returns the whole row of data
- Implementing fuzzy query with dataframe
猜你喜欢

Yolov5 tensorrt acceleration
![[classic example] binary tree recursive structure classic topic collection @ binary tree](/img/39/0319c4be43716f927b9d98d89f7655.jpg)
[classic example] binary tree recursive structure classic topic collection @ binary tree

February 12 relativelayout

RT thread analysis - object container implementation and function

Easy to understand I2C protocol

04. Project blog log

F12 solve the problem that web pages cannot be copied

Lepton 无损压缩原理及性能分析

Graduation design game mall

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
随机推荐
05. 博客项目之安全
First acquaintance with CDN
【云原生】3.1 Kubernetes平台安装KubeSpher
MySQL advanced learning summary 9: create index, delete index, descending index, and hide index
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
Configuration file converted from Excel to Lua
EditorUtility.SetDirty在Untiy中的作用以及应用
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
Drive development - the first helloddk
Pagoda configuration mongodb
Promotion hung up! The leader said it wasn't my poor skills
初识CDN
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
Class inheritance in yyds dry inventory C
[untitled]
Nacos - TC Construction of High available seata (02)
RT thread analysis log system RT_ Kprintf analysis
Jvxetable用slot植入j-popup
剑指 Offer II 039. 直方图最大矩形面积
TCP three handshakes you need to know