Brush wins the offer title: Rectangular coverage problems

We can use a small rectangle 2 * 1 sideways or vertically to cover a larger rectangle. Will the use of eight 2 small rectangle 1
non-overlapping coverage of a 2
large rectangular 8, how many ways are there?

*                     * * * * * * * *
*                     * * * * * * * *

* The schematic as a rectangle.

Obviously, such a big problem that we can break down into smaller problems, such as, for this rectangle 2 * 8 placement method f (8), if for the first time on end, then left f (7 ) placement methods need to be considered, however, for the first time, also can be placed sideways, then left the placement method has f (6) need to be considered. Thus, f (8) There are two modes, i.e., f (8) = f (7) + f (6). This is a Fibonacci number of columns cut problem, f (1) = 1, f (2) = 2, accordingly, we can write the program.

class Solution:
    def rectangle_cover(self, number):
        if number == 0:
            return
        if number == 1:
            return 1
        if number == 2:
            return 2
        if number > 2:
            tempArray = [1, 2]

            for i in range(3, number + 1):
                tempArray[i%2] = tempArray[0] + tempArray[1]
        return tempArray[number % 2]
He published 189 original articles · won praise 233 · views 360 000 +

Guess you like

Origin blog.csdn.net/Einstellung/article/details/90020345