当前位置:网站首页>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
边栏推荐
- 用StopWatch 统计代码耗时
- A little knowledge of CPU, disk and memory
- 01. 开发博客项目之项目介绍
- 2022 half year summary
- Cve-2019-11043 (PHP Remote Code Execution Vulnerability)
- MySQL advanced learning summary 9: create index, delete index, descending index, and hide index
- Realize a binary read-write address book
- Postman assertion
- Figure database ongdb release v-1.0.3
- 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
猜你喜欢
Easy to understand I2C protocol
Pix2pix: image to image conversion using conditional countermeasure networks
05. 博客项目之安全
[mask requirements of OSPF and Isis in multi access network]
Vulhub vulnerability recurrence 68_ ThinkPHP
Ora-01779: the column corresponding to the non key value saving table cannot be modified
用StopWatch 统计代码耗时
[leetcode] 18. Sum of four numbers
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
Ad20 is set with through-hole direct connection copper sheet, and the bonding pad is cross connected
随机推荐
Principle and performance analysis of lepton lossless compression
集合详解之 Collection + 面试题
Vulhub vulnerability recurrence 69_ Tiki Wiki
Graduation design game mall
Pix2pix: image to image conversion using conditional countermeasure networks
Biscuits (examination version)
UCF(暑期团队赛二)
[noip2009 popularization group] score line delimitation
Nacos - TC Construction of High available seata (02)
Golang -- TCP implements concurrency (server and client)
Huawei equipment is configured with OSPF and BFD linkage
Figure database ongdb release v-1.0.3
指針經典筆試題
Cve-2019-11043 (PHP Remote Code Execution Vulnerability)
Jvxetable用slot植入j-popup
Ad20 is set with through-hole direct connection copper sheet, and the bonding pad is cross connected
指针经典笔试题
Postman pre script - global variables and environment variables
Hyperledger Fabric2. Some basic concepts of X (1)
[leetcode16] the sum of the nearest three numbers (double pointer)