当前位置:网站首页>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 
边栏推荐
- Display the remaining valid days according to the valid period
- Audio and video engineer (Preliminary) (I) basic concepts of audio and video
- RuntimeError:Input and parameter tensors are not at the same device, found input tensor at cuda:0 an
- day5-x
- Xshell startup encountered "unable to continue code execution because mfc110.dll cannot be found"
- 通俗理解时域采样与频域延拓
- Shell basic syntax -- arithmetic operation
- Domain name mapping to specified IP
- Error: ER_ NOT_ SUPPORTED_ AUTH_ MODE: Client does not support authentication protocol requested ... ...
- [essence] explain in detail the memory management mechanism in QT
猜你喜欢
![(node:22344) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permit](/img/c1/d56ec09663857afa52f20848aeadac.png)
(node:22344) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permit

IP, DNS, domain name, URL, hosts

Display the remaining valid days according to the valid period

Composition of box model

Detailed explanation of iSCSI (V) -- actual operation of iSCSI client configuration

xshell启动遇到“由于找不到mfc110.dll,无法继续执行代码的解决方法”

Build personal blog and web.

2022 melting welding and thermal cutting test questions and answers

域名映射到指定IP
![[GUI development] browsing function implementation model of image processing software](/img/37/2162a6047682b9cfc9b8b7c2488068.jpg)
[GUI development] browsing function implementation model of image processing software
随机推荐
处理异常数据
Background fixing effect
Jump to an interface at a specified time. (Web Development)
Flink passes in custom parameters or profiles
正则校验用户名
When converting tensor to ndarray in tensorflow, the run or Eval function is constantly called in the loop, and the code runs more and more slowly!
Regularization to limit the number of digits after the decimal point of an input number
Chapter IV - first procedure
Composition of box model
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
(十三)文本渲染Text
2022.6.9-----leetcode.497
Flink CheckPoint : Exceeded checkpoint tolerable failure threshold
ERROR 1630 (42000): FUNCTION a.avg does not exist. Check the ‘Function Name Parsing and Resolution‘
Domain name mapping to specified IP
day5-x
QT realizes multi screen and multi-resolution adaptation
Audio and video related links
Judge whether the object is empty
[dynamic memory management] malloc & calloc and realloc and written test questions and flexible array
