当前位置:网站首页>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
边栏推荐
- GoLand IDE and delve debug Go programs in kubernetes cluster
- LeetCode之三步问题
- Source code analysis of kubernetes' process of deleting pod
- No suspense about the No. 1 Internet company overtime table
- Co process, asyncio, asynchronous programming
- Section 9: dual core startup of zynq
- 挖财注册开户靠谱吗?安全吗?
- ES6 use of return in arrow function
- HJ prime factor
- 卸载重装最新版mysql数据库亲测有效
猜你喜欢

Kubernetes理论基础

"Three routines" of digital collection market

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

PLC -- Notes

Analyze 5 indicators of NFT project

Hash slot of rediscluster cluster cluster implementation principle

At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions

ACM笔记

Application of XOR. (extract the rightmost 1 in the number, which is often used in interviews)

扩展Prometheus的解决方案thanos的简介和几个月使用心得
随机推荐
分析 NFT 项目的 5 个指标
XML serialization backward compatible
LeetCode之三步问题
Ambari (VII) --- ambari integrated hue4.2 document (valid for personal test)
Design and implementation of spark offline development framework
Redis one master multi slave cluster setup
Makefile
Section 5: zynq interrupt
asp. Net datalist when there are multiple data displays
asp. Net registration page
Understanding of OPC protocol
kubelet驱逐机制的源码分析
es数据导出csv文件
Section 8: DMA of zynq
Redis implements distributed locks
GoLand IDE and delve debug Go programs in kubernetes cluster
asp. Net error "/" server error in the application. String or binary data would be truncated. The statement...
NDK cross compilation
Flex layout
Tencent continued to lay off staff in the second half of the year, and all business groups reduced by at least 10%. What do you think of this? Followers
