(DP) rectangular footprint

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?

https://www.nowcoder.com/questionTerminal/72a5a919508a4251859fb2cfb987a0e6

Practice: the first with a small rectangle to cover 1 2 * 2 * i large rectangle at the leftmost of the two options: put on end or sideways. When on end, the right left region (i-1) * 2, cover the approach in this case referred to as f [i-1]; sideways put a small rectangle in the upper left 2 * 1, in which case lower left corner of the small rectangle must put sideways 2 * 1, in which case the right left area 2 * (i-2), the coating method in this case referred to as f [i-2], is f [i] = f [i-1] + f [i-2].

class Solution {
public:
    int rectCover(int number) {
        int f[number+1];
        f[0] = 0;
        f[1] = 1;
        f[2] = 2;
        for(int i=3; i<=number; i++){
            f[i] = f[i-1]+f[i-2];
        }
        return f[number];
    }
};

 

Guess you like

Origin www.cnblogs.com/Bella2017/p/11823867.html