Recently I started to study algorithms and encountered this very interesting topic, because I reviewed the Fibonacci sequence from it, and through some information, I checked the official website of the Chinese Academy of Sciences and read many popular science articles. Dig deeper and you can see a lot of things.
In line with the original intention of sharing, I organize this article to share with you. The topic itself is not difficult. Welcome to communicate together. The algorithm guys please do not spray, thank you.
Go To Topic.
This LeetCode question 70, climbing stairs , the questions are as follows:
Suppose you are climbing stairs. It takes n steps to reach the top of the building. You can climb 1 or 2 steps each time. How many different ways do you have to climb to the top of a building?
Everyone can think about first.
Process analysis
In this question, you can go to level 1 or level 2 at a time, so we have 3 ways to move:
- Walk arbitrarily throughout the whole journey, such as all level 1 walks;
- Take the first step at will, and only take 1 level at the last step;
- Walk at will, the last step is only 2 levels;
I drew a few pictures for everyone to understand, as follows:
The first method will not be introduced in detail.
The second way of walking, the penultimate step is as follows, there are two ways of 1 step and 2 steps:
The third move, the penultimate move is as follows, there are also two ways of 1 step and 2 steps:
The above process describes the movement of each layer starting from the last layer.
At the last step, there are two methods, one step and two steps, which can be understood as only one step or two steps to reach the last layer.
- When the last step is step 1, it starts from layer n-1;
- When the last step is 2 steps, it starts from layer n-2;
To understand this process again, the number of moves of the nth layer is the sum of the number of moves of the n-1th layer and the n-2th layer.
If you still don’t understand, you can look at the previous picture again.
Inductive analysis
Of course, in case of indecision, the induction method starts, we can list several situations for analysis:
Number of steps | Number of moves | How to move |
---|---|---|
1 | 1 | 1 |
2 | 2 | 11、2 |
3 | 3 | 111、12、21 |
4 | 5 | 1111、112、121、211、22 |
5 | 8 | 11111、1112、1121、1211、2111、221、212、122 |
... | ... | ... |
It can be found that there is a simple rule. When the number of steps is n, the number of steps is equal to the number of steps of n-1 and the number of steps of n-2.
Record it as: f(n)=f(n-1)+f(n-2)
.
Fixed 1 moving method on the first layer;
Fixed two ways of moving on the second layer;
...
The number of moves on the 5th floor is equal to the number of moves on the 4th floor plus the number of moves on the 5th floor.
After understanding the rules of the entire process, we can code much simpler:
Solution 1: cyclic accumulation calculation
The result can be obtained by simple loop accumulation:
const climbStairs = (n = 1) => {
if(n <= 2) return n;
let res = 0, n1 = 1, n2 = 2; // n1 表示前 2 项,n2 表示前 1 项
for(let i = 3; i<= n; i++){ // 前两项值固定,因此从第 3 项开始循环
res = n1 + n2;
n1 = n2;
n2 = res;
}
return res;
}
The number of moves in the 6th layer under the test:
climbStairs(6); // 13
Solution 2: Recursive calculation
According to f(n)=f(n-1)+f(n-2)
, this method is even simpler:
const climbStairs = (n = 1) => {
if(n <= 2) return n;
return climbStairs(n-1) + climbStairs(n-2);
}
The number of moves in the 6th layer under the test:
climbStairs(6); // 13
This method is relatively concise and easy to understand, but the recursion is time-consuming, and it is prone to prompts that LeetCode exceeds the time limit.
Solution 3: Use array characteristics
Using f(n)=f(n-1)+f(n-2)
, first preset the first 2 items, then start the loop, and finally return to the last item of the array:
const climbStairs = n => {
let result = [1,2];
for (let i = 2; i < n; i++) {
result.push(result[i-1] + result[i-2]);
}
return result[n-1];
};
Solution 4: Take advantage of the new features of JavaScript ES6
Use the array structure assignment operation: [a, b] = [c, d]
:
const climbStairs = n => {
let a = b = 1;
for (let i = 0; i < n; i++) {
[a, b] = [b, a + b];
}
return a;
};
Of course, everyone has other solutions, welcome to discuss together~
Expand knowledge: you can take 1, 2, 3 steps each time
Here is one more step and you can take 3 steps. At this time, the last step will have the following conditions:
- When the last step is step 1, it starts from layer n-1;
- When the last step is 2 steps, it starts from layer n-2;
- When the last step is 3 steps, it starts from layer n-3;
Modify the previous solution, it is still the same:
const climbStairs = (n = 1) => {
if(n <= 2) return n;
if(n == 3) return 4;
return climbStairs(n-1) + climbStairs(n-2) + climbStairs(n-3);
}
The number of moves in the 6th layer under the test:
climbStairs(6); // 24
Expanding Knowledge: Fibonacci Sequence
The content mainly on questions similar Fibonacci number (Fibonacci Sequence) calculations, if you do not know what is a Fibonacci number, briefly explain here, is also recommended Lee Wing-lok, the teacher to explain Fibonacci's class .
It was first introduced by the mathematician Leonardoda Fibonacci using rabbit reproduction as an example. The number sequence is roughly as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34....
Observing carefully, we can find a rule: starting from the third item, the value of each item is equal to the sum of the first two .
In nature, there are many arrangements of Fibonacci numbers, such as an ordinary tree whose branches grow like the following:
(Picture source network)
It can be seen that the number of branches in each layer is 1, 2, 3, 5, 8, ... arranged in order. Of course there are many others:
(Various Pebonacci spirals in nature, the picture comes from the Internet)
According to the law of Fibonacci sequence, the formula f(n)=f(n-1)+f(n-2)
. Similar to what we listed earlier.
Summarize
This question itself is not difficult, but if the process and rules are not clarified, it is easy to fall into the hole and write redundant code. This article only lists 4 simple implementation methods, if you have other implementation methods, welcome to discuss together, haha.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。