当前位置:网站首页>Three step problem of leetcode
Three step problem of leetcode
2022-06-28 07:53:00 【Little light_】
subject
Three step problem . There is a child going up the stairs , The stairs have n Steps , Children can go to 1 rank 、2 Step or 3 rank . Implement a way , Calculate how many ways a child can go up stairs . The results could be big , You need to model the results 1000000007.
Example 1:
Input :n = 3
Output :4
explain : There are four ways to walk
Example 2:Input :n = 5
Output :13source : Power button (LeetCode)
link :https://leetcode.cn/problems/three-steps-problem-lcci
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas
Like a frog jumping on a step , First write the results of the previous steps :
Through these results , You can find the law :
f(n)=f(n-1)+f(n-2)+f(n-3) (n≥3, And f(0)=f(1)=1,f(2)=2), With this rule , The code is easy to write !Maybe , Some of them will say , How do you know that you can get the right result by finding the rules from the beginning ? In fact, I didn't know at first , It is with a try attitude ( Because I was doing “ Frogs jump the steps ” I learned this method when I was , And this topic is very similar to it , So it's easy to think of this method by analogy ! therefore , The kingcraft of problem solving : Do more , How to summarize .)
Source code
class Solution:
# It is similar to climbing only one or two stairs at a time , Find out the rules , Find out :f(n)=f(n-1)+f(n-2)+f(n-3) (n≥3, And f(0)=f(1)=1,f(2)=2)
def waysToStep(self, n: int) -> int:
if n == 1: return 1
if n == 2: return 2
n0, n1, n2 = 1, 1, 2
for i in range(n - 2):
n0, n1, n2 = n1, n2, (n0 + n1 + n2) % 1000000007
# print(n2)
return n2Through screenshots

Synchronous update in personal blog system :LeetCode The third step
边栏推荐
猜你喜欢

Sentinel mechanism of redis cluster

Rediscluster cluster mode capacity expansion node

Safety training is the greatest benefit for employees! 2022 induction safety training for new employees

Ambari (V) ---ambari integrated Azkaban (valid for personal test)

MySQL installation and environment variable configuration

8 张图 | 剖析 Eureka 的首次同步注册表

sql分析(查询截取分析做sql优化)

asp. Net registration page

flex布局

卸载重装最新版mysql数据库亲测有效
随机推荐
Ice, protobuf, thrift -- Notes
2021 programming language ranking summary
Resizing node of rediscluster cluster cluster mode
软件测试与质量期末复习
es6箭头函数中return的用法
asp. Net to search products and realize paging function
Airflow2.x distributed deployment DAG execution failure log cannot be obtained normally
Soft exam -- software designer -- afternoon question data flow diagram DFD
GPIO configuration of SOC
flutter 实现摇一摇功能
Software testing and quality final review
SOC clock configuration
Study notes 22/1/17
What is the lifecycle of automated testing?
Kubernetes cluster command line tool kubectl
Localization SoC development plan
Porting ucosiii to stm32f429
asp. Net registration page
SOC serial port configuration
Design of DSP image data stream
