当前位置:网站首页>Sword finger offer:[day 8 dynamic planning (simple)] --- > frog jumping on steps
Sword finger offer:[day 8 dynamic planning (simple)] --- > frog jumping on steps
2022-06-12 08:57:00 【Love you, little pig】
List of articles
One 、 Title Description
A frog can jump up at a time 1 Stepped steps , You can jump on it 2 Stepped steps . Ask the frog to jump on one n How many jumps are there in the steps .
The answer needs to be modelled 1e9+7(1000000007), If the initial result of calculation is :1000000008, Please return 1.
Example 1:
Input :n = 2
Output :2
Example 2:
Input :n = 7
Output :21
Example 3:
Input :n = 0
Output :1
Tips :0<=n<=100
Two 、 Thought analysis
notes : Some contents and pictures in the train of thought analysis refer to the solutions of your predecessors , Thank them for their selfless dedication
Ideas
Let's jump up
nThe steps havef(n)Jump . Of all the jumps , There are only two situations for the frog's last step : Jump on1Grade or2Stepped steps .
When it comes to1Stepped steps : Remnantn-1A stair , There are a total off(n-1)Jump
When it comes to2Stepped steps : Remnantn-2A stair , There are a total off(n-2)Jumpf(n)Is the sum of the above two situations , namelyf(n)=f(n-1)+f(n-2), The above recurrence property is Fibonacci sequence . This problem can be transformed into finding the number of FibonaccinItem value , The only difference is that the starting number is different .
The problem of frog jumping on the steps :f(0)=1,f(1)=1,f(2)=2
Fibonacci series question :f(0)=0,f(1)=1,f(2)=1
Complexity analysis :
Time complexity O ( N ) \rm{O(N)} O(N): Calculationf(n)Need circulationnTime , The calculation operation in each cycle usesO(1)
Spatial complexity O ( 1 ) \rm{O(1)} O(1): Several flag variables use constant size extra space
3、 ... and 、 The overall code
The overall code is as follows
int numWays(int n){
int tmp;
int a = 1;
int b = 1;
for(int i = 0; i < n; i++){
tmp = (a + b) % 1000000007;
a = b;
b = tmp;
}
return a;
}
function , The test passed 
边栏推荐
- 【无标题】Task3 多路召回
- ip、DNS、域名、URL、hosts
- 2022 low voltage electrician retraining question bank and online simulation examination
- 43 cas d'analyse du réseau neuronal MATLAB: chapitre 7 régression du réseau RBF - - réalisation de la régression fonctionnelle non linéaire
- day5-x
- Flink CheckPoint : Exceeded checkpoint tolerable failure threshold
- [dynamic memory management] malloc & calloc and realloc and written test questions and flexible array
- Popular understanding of time domain sampling and frequency domain continuation
- About weights exercise
- Make a simple page with the websql database of HTML5:
猜你喜欢

第四章-第一个程序

了结非对称密钥

机器学习笔记 - 循环神经网络备忘清单

Background position position NOUN

Composition of box model

Display the remaining valid days according to the valid period

Binlog in mysql:
![[advanced pointer 2] array parameter transfer & pointer parameter transfer & function pointer & function pointer array & callback function](/img/90/447d601a8c338cdd5a6674a2dc59ae.png)
[advanced pointer 2] array parameter transfer & pointer parameter transfer & function pointer & function pointer array & callback function

mySql学习记录——二、mySql建表命令

Application method of new version UI of idea + use method of non test qualification and related introduction
随机推荐
第八章-数据处理的两个基本问题
Shell基本语法--数组
《MATLAB 神经网络43个案例分析》:第7章 RBF网络的回归--非线性函数回归的实现
通俗理解时域采样与频域延拓
2022.6.9-----leetcode. four hundred and ninety-seven
Judge whether the object is empty
RuntimeError:Input and parameter tensors are not at the same device, found input tensor at cuda:0 an
Introduction to Chang'an chain node certificate, role and authority management
Knee joint
动态创建表单并提交
Analysis of 43 cases of MATLAB neural network: Chapter 7 regression of RBF Network -- Realization of nonlinear function regression
When the uniapp page jumps with complex data parameters.
[compilation principle] understand BNF
The difference between deep copy and shallow copy
Convert spaces to < br / > newline labels
Engineers learn music theory (I) try to understand music
Background attribute compound writing
【字符集六】宽字符串和多字节字符互转
Wechat applet image saving function
[new planning]
