Home > Article > Backend Development > PHP implementation of offer jumping steps example
This article mainly shares with you an example of how to implement offer jumping in PHP. I hope it can help you. Let's look at an example first. A frog can jump up one step at a time, or it can jump up two steps... It can also jump up n steps. Find out how many ways the frog can jump up an n-level staircase.
Idea: Based on the Fibonacci sequence:
F(N)=F(N-1)+F(N-2)+F(N-3)+F (N-4)+.....F(2)+F(1)
F(N-1)=F(N-2)+F(N-3)+F(N -4)+.....+F(2)+F(1)
Subtract the two to get: F(N-1)=2*F(N-1)
<?php function jumpFloorII($number) { $arr[0]=0; $arr[1]=1; $arr[2]=2; for($i=3;$i<=$number;$i++) { $arr[$i]=2*$arr[$i-1]; } return $arr[$number]; }
Related recommendations:
There are n levels in a step. If you can jump 1 level or 2 levels at a time, find the total number
The above is the detailed content of PHP implementation of offer jumping steps example. For more information, please follow other related articles on the PHP Chinese website!