topic
Links:
stairs
Source: Cattle-off network
There is a child is on the staircase, an n-order level, a child may be on the order of 1, 2-order, third-order. Implement a method to calculate how many ways there are children upstairs. To prevent spilling, leave the result Mod 1000000007
Given a positive integer int n, return to a number representative of the number of stairs manner. N less than or equal to ensure 100 000.
Test Example 1:
1
返回:1
Test Sample 2:
3
返回:4
Test Sample 3:
4
返回:7
After reading the title hearts of a move! This question I've ever seen. Well, in fact, we do have a similar problem, but also a child climbing stairs, but that kid move 1 or 2 steps once. This child is 1, 2 or 3 steps.
analysis
Idea: In fact, the same idea. You can use recursion to write.
If the child has gone one step the first step, the remaining (n-1) th stepped down,
if a child Step 2 steps away, the remaining (n-2) th down step,
if the first child step away three steps, then left (n-3) a step away.
The first step in a child will these three cases, and then the rest of the walk is actually three add up!
So recursion can be written:
no loss is you (recursive) simple
//递归写法
public static int getCount(int n){
if(n == 1){
return 1;
}else if(n == 2){
return 2;
}else if(n == 3){
return 4;
}else {
return getCount(n-1)+getCount(n-2)+getCount(n-3);
}
}
But recursive computation is notoriously slow. So we have to change the non-recursive. But also to avoid data overflow.
Did people can go 1 or 2-step problem child, we can find
a combination of that child and the walk is the Fibonacci columns:
Stairs | Hashiho |
---|---|
1 | 1 |
2 | 2 |
3 | 3 |
4 | 5 |
5 | 8 |
… | … |
We will try the same this child can walk 3 steps and the walk to sum up:
Stairs | Hashiho |
---|---|
1 | 1 |
2 | 2 |
3 | 4 |
4 | 7 |
5 | 13 |
… | … |
Found not: You can take the kids to go three-step method, it is also similar to the Fibonacci series of Number of. The Fibonacci first two columns is obtained by adding a third element.
And this number is obtained by adding the fourth column of the first three. And so on ...
Then we can try to write code:
//计算
public static int getCounts(int n){
if(n == 1){
return 1;
}else if(n == 2){
return 2;
}else if(n == 3){
return 4;
}else {
int first = 1;
int second = 2;
int third = 4;
int forth = 0;
while (n - 3 > 0) {
forth = ((first + second)%1000000007+third%1000000007)%1000000007;
first = second;
second = third;
third = forth;
n--;
}
return forth;
}
}
Here it is more difficult to understand:
forth = ((first + second)%1000000007+third%1000000007)%1000000007
This is an original sentence is this:
because in order to prevent data overflow, subject of the request would
result mod 1000000007
forth = (first + second+third)%1000000007;
But this will still overflow, so we need to transform!
Cattle off the comments area found the explanation of this sentence
plus the online inquiry results:
(A + b)% c == (A + b% c% c)% c
so we can (first + second + third )% 1000000007
(above) written!
So far the main algorithm is finished.
Here is the code adopted.
Code
import java.util.*;
public class GoUpstairs {
//计算
public static int getCounts(int n){
if(n == 1){
return 1;
}else if(n == 2){
return 2;
}else if(n == 3){
return 4;
}else {
int first = 1;
int second = 2;
int third = 4;
int forth = 0;
while (n - 3 > 0) {
forth = ((first + second)%1000000007+third%1000000007)%1000000007;
first = second;
second = third;
third = forth;
n--;
}
return forth;
}
}
public int countWays(int n) {
// write code here
return getCounts(n);
}
}