当前位置:网站首页>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 n2
Through screenshots
Synchronous update in personal blog system :LeetCode The third step
边栏推荐
- 本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
- Is it reliable for securities companies to register and open accounts? Is it safe?
- flex布局
- Software testing and quality final review
- 7-1 understand everything
- Kubernetes理论基础
- Co process, asyncio, asynchronous programming
- 协程、asyncio、异步编程
- No suspense about the No. 1 Internet company overtime table
- Block transmission by golang gin framework
猜你喜欢
flex布局
Kubelet garbage collection (exiting containers and unused images) source code analysis
ES6 use of return in arrow function
Sentinel mechanism of redis cluster
推荐系统系列精讲(第五讲): 排序模型的调优实践
PLC -- Notes
asp. Net datalist when there are multiple data displays
kubernetes删除pod的流程的源码简析
Section Xi. Axi of zynq_ Use of DMA
Software design of resistance test board
随机推荐
Today's notes 22/1/7
HJ质数因子
【js】-【DFS、BFS应用】-学习笔记
HJ字符个数统计
Safety training is the greatest benefit for employees! 2022 induction safety training for new employees
打新债注册开户靠谱吗?安全吗?
软件测试与质量期末复习
自动化测试的生命周期是什么?
Unity-UI-shadow组件
flex布局
HJ成绩排序
GPIO configuration of SOC
Study notes 22/1/17
sql分析(查询截取分析做sql优化)
GoLand IDE and delve debug Go programs in kubernetes cluster
Open62541 import nodeset file directly
基金的投资交易与结算
HJ base conversion
HJ string sort
Unity UI shadow component