https://github.com/PhillipHuang2017/SwordOffer
10. The rectangular footprint
Title Description
- We can use a small rectangle 2 * 1 sideways or vertically to cover a larger rectangle. Will the small rectangle of n 2 * 1 coverage without overlap a large rectangle 2 * n, a total of how many ways?
Problem-solving ideas
- Firstly, the simple case:
f(1) = 1, f(2) = 2
. - When
n >= 3
, the look of the first small rectangular display mode, if the first small rectangle is placed sideways, and then the rectangular section determines the following, must also be put sideways, corresponding to eliminate the 2 * 2 region, left behind 2 * (n-2) of the rectangular area; if the first is a small rectangle on end, then back on the well 2 * (n-1) of the rectangular region. So getf(n) = f(n-1) + f(n-2), n >= 3
. - Therefore, it is also a Fibonacci column problem.
C ++ code
class Solution {
public:
int rectCover(int number) {
if(number < 1){
return 0;
}
int a = 1, b = 1;
while(--number){
b += a;
a = b - a;
}
return b;
}
};