当前位置:网站首页>Leetcode skimming: dynamic planning 02 (climbing stairs)
Leetcode skimming: dynamic planning 02 (climbing stairs)
2022-07-22 10:29:00 【Taotao can't learn English】
70. climb stairs
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 ?
Be careful : Given n Is a positive integer .
Example 1:
- Input : 2
- Output : 2
- explain : There are two ways to climb to the top .
- 1 rank + 1 rank
- 2 rank
Example 2:
- Input : 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
* The first stair 1 * The second stair 2 11 or 2 * The third stair 3 111 12 213 * The fourth stair 5 1111 112 121 211 22 * The fifth stair 8 11111 1112 1121 1211 122 2111 212 221
Algorithm implementation steps :
- determine dp The meaning of arrays and subscripts
– Definition dp[i] To climb the third i How many options are there for steps .- Determine the state transfer equation
– Because you can only climb 1 perhaps 2 A stair , therefore , The plan to climb the current level should be the sum of the previous two states , namely dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ]To understand this wave in depth : dp[i] How to achieve , By dp [ i - 1 ] Move a bit or dp [ i - 1 ] Move two bits to get . The mobile 1 grid All possible and mobile 2 All the possibilities of lattice . Namely dp [ i ] Value . How many in all dp [ i - 1 ] and dp [ i - 2 ] It's possible , Recursion , Or dynamic programming is calculated according to the previous result , It is similar to the result value of each layer of array cache done before .
- Initialization status
– i = 0 Level is still downstairs , Didn't start climbing
– i = 1 Level 1 starts climbing , namely dp[1]=1
– i = 2 level , Yes 2 Kind of ,dp[2]=2- traversal order
– From the state transition equation dp[i] yes dp[i-1] and dp[i-2] Turn around , So go from front to back- Return value
– The first n Take the stairs and return dp[n]
package com.programmercarl.dynamic;
/** * @ClassName ClimbStairs * @Descriotion TODO * @Author nitaotao * @Date 2022/7/21 8:18 * @Version 1.0 * https://leetcode.cn/problems/climbing-stairs/ * 70. climb stairs **/
public class ClimbStairs {
public int climbStairs(int n) {
if (n <= 1) {
return n;
}
/** * The first stair 1 * The second stair 2 11 or 2 * The third stair 3 111 12 21 * The fourth stair 5 1111 112 121 211 22 * The fifth stair 8 11111 1112 1121 1211 122 2111 212 221 */
// dp[0] no need
int[] dp = new int[n + 1];
dp[1] = 1;
dp[2] = 2;
int first = dp[1];
int second = dp[2];
for (int i = 3; i <= n; i++) {
// Like Fibonacci series
// According to the first two, the third
int cur = first + second;
dp[i] = cur;
first = second;
second=cur;
}
return dp[n];
}
}
边栏推荐
- AtCoder Beginner Contest 260 E - At Least One
- [Basic] Pointer in C
- std::thread 與類對象結合
- 一些个人理解
- 黑客赏金猎人平台之Immunefi
- [interview: concurrent Article 20: multithreading: multiple locks]
- Is it safe for Huatai Securities to open an account in 2020? Is it a regular securities firm
- 2022杭电多校 第一场 个人题解(ABCDIHK)
- Wechat applet development learning 6 (foundation strengthening using NPM package and global data sharing and subcontracting [tab bottom column case improvement])
- 微信小程序开发学习6(基础加强之使用npm包和全局数据共享及分包【Tab底栏案例改进】)
猜你喜欢
El table column nested El table column, horizontal scrolling bug of multi-level header
1307_ Summary of crystal oscillator test in embedded design
[cicadaplayer] vs2019 (v142) x64 ffmepg: error c2169: "lrintf": internal function, cannot be defined
Some personal understanding
OSPF comprehensive experiment
光明正大的水贴来自考研人对暑假的感悟
Unity C: use this keyword to expand class functions
上门预约程序公众号模块 完美版
Unity Scrollview scroll to the specified position
Door to door appointment procedure official account module perfect version
随机推荐
Using tutorial 1- create X20 project and light LED lights
How to add an operator in ONEFLOW
网页保存为pdf神器(可自定义编辑)—Print Edit WE
指针的理解与操作
New product release: VGA display driver module for bus video monitoring
金仓数据库KingbaseES安全指南--3.1. 用户管理
[explanation of PTA program design topic] [English version] [6-5 use function to verify Goldbach conjecture]
MySQL transaction operation and locking mechanism
2022年最新湖北建筑八大员(机械员)模拟考试题库及答案
元宇宙时代到来,Soul张璐团队如何打造全新社交体验?
2022杭电多校 第一场 个人题解(ABCDIHK)
11-GuliMall 后台管理中商品系统的分类维护
2022 open source PHP message feedback management system v2.0
启动,关闭,查看MySQL服务(Linux)
测试环境建设的基本原则
Energy principle and variational method note 08: derivation of virtual work principle
Summary of some experiences in the process of R & D platform splitting
Smart phone antenna tuning
Collection (properties)
2022.7.21 特殊矩阵压缩