Offer # 10 wins the rectangular footprint (Analysis)

Topic Source: Cattle passenger network - Offer to prove safety topics
topics Address: rectangular footprint

Title Description

We can use 2 1 2*1 small rectangular sideways or vertically to cover a larger rectangle. Will the use of n 2 1 2*1 small rectangles without overlapping a cover 2 n 2*n large rectangle, a total of how many ways?

For example n = 3, the 2 3 2*3 rectangular blocks has three kinds of coating method:
1

Analytical title

In fact, this type of problem, if you really do not know how to start, you may wish to consider the enumeration, in front of the small number of enumerated first, then guess what the law Yes. General formula may be considered are 2 n 1 2^{n-1} , Fibonacci number, the polynomial of n-related, the number of combinations, the number of Cattleya, Euler function ......

This look at this question, we first n n the case of a relatively small first enumerated in the following table:

n 1 2 3 4 5 6
result 1 2 3 5 8 13

Look, do not lie to you now! This is not a Fibonacci number do? I do not pit you, but to provide an idea when you really can not start, do not quick to write What to enumerate these circumstances ......


Below is a positive solution time:

Refer to the discussion forums Follow explain the idea of big brother, suppose we have a 2 n 2*n large rectangle, when n = i n=i method when there is f ( i ) f(i) a seed, we first consider the first 2 1 2*1 small rectangle pendulum method. There are the following two situations:

  • Case 1: the following FIG placed on end, then the rest is formed of a rectangular 2 ( n 1 ) 2*(n-1) a large rectangle, the coating method in this case has f ( n 1 ) f(n-1) species.
*
*
  • Case 2: sideways placed below, in order to cover the space below it, the second 2 1 2*1 small rectangle must be placed sideways at the bottom of it ,, then the rest of it forms a rectangle 2 ( n 2 ) 2*(n-2) The large rectangle, the coating method in this case has f ( n 2 ) f(n-2) species.
* *
- -

So, we can know f ( n ) = f ( n 1 ) + f ( n 2 ) f(n)=f(n-1) + f(n-2) , recombination n 2 0 n-2 \leq0 case, we can obtain the following recursive friends ~

f ( n ) = { 1 , n=1 2 , n=2 f ( n 1 ) + f ( n 2 ) , n>2 f(n)= \begin{cases} 1&, \text{n=1} \\ 2 &,\text{n=2} \\ f(n-1) + f(n-2) &,\text{n>2} \end{cases}

这个递推式看烂了,这里就简单写常用的两种实现方式,详情可以参考上篇博客解法:斐波那契数列(四种解法)

方法一:
面试别写型递推版实现,时间复杂度 O ( 2 n ) O(2^n)

public class Solution {
    public int RectCover(int n) {
        if (n < 3) {
            return n;
        }
        return RectCover(n - 1) + RectCover(n - 2);
    }
}

方法二:
面试推荐型,自底向上型循环求解,时间复杂度为 O ( n ) O(n)

public class Solution {
    public int RectCover(int n) {
        if (n == 0) return 0;
        int a = 1, b = 1;
        for (int i = 1; i < n; i++) {
            a = a + b;
            b = a - b;
        }
        return a;
    }
}

如果本文对你有所帮助,要记得点赞哦~

发布了83 篇原创文章 · 获赞 167 · 访问量 5万+

Guess you like

Origin blog.csdn.net/weixin_42292229/article/details/104552703