Old Wei wins the offer to take you to learn --- Brush title series (9 metamorphosis jump stairs)

9. metamorphosis jump stairs

problem:

A frog can jump on a Class 1 level, you can also hop on level 2 ...... n It can also jump on stage. The frog jumped seeking a total of n grade level how many jumps.

solve:

thought:

About this problem, provided that jumps n th there will be a level of order n. analyse as below:

f(1) = 1

F (2) = F (2-1) F + (2-2) F (2-2) represents a second-order frequency hop 2 order.

f(3) = f(3-1) + f(3-2) + f(3-3)

f(n) = f(n-1) + f(n-2) + f(n-3) + … + f(n-(n-1)) + f(n-n)

Description:

  • Where f (n) represents n-th time step has 1,2, ... n-th order of hop count method.

  • n = 1, only one kind of jumps, f (1) = 1

  • When n = 2, there will be two jump mode, a first-order or second-order, which return to the problem (1), f (2) = f (2-1) + f (2-2)

  • For n = 3, there will be three kinds of jump mode, the first-order, second order, third order,

    Then that the first stage out of the back rest 1: f (3-1); 2 out of the first order, the remaining f (3-2); 3 first order, the remaining f (3-3)

    It was concluded that f (3) = f (3-1) + f (3-2) + f (3-3)

  • When n = n, there are n hops manner, the first-order, second order ... n order, concluded:

    f(n) = f(n-1)+f(n-2)+…+f(n-(n-1)) + f(n-n) => f(0) + f(1) + f(2) + f(3) + … + f(n-1)

  • From the above is already a conclusion, but for simplicity, we can continue to simplify:

    f(n-1) = f(0) + f(1)+f(2)+f(3) + … + f((n-1)-1) = f(0) + f(1) + f(2) + f(3) + … + f(n-2)

    f(n) = f(0) + f(1) + f(2) + f(3) + … + f(n-2) + f(n-1) = f(n-1) + f(n-1)

    It can be drawn:

    f(n) = 2*f(n-1)

Then the rest is up and before the recursive + dynamic programming as a

python code:

class Solution:
    def __init__(self):
        self.dynamic=[None]*100
    def jumpFloorII(self, number):
        # write code here
        result=self.dynamic[number]
        if(result==None):
            if(number==1):
                result=1
            elif(number==2):
                result=2
            else:
                result=self.jumpFloorII(number-1)*2
            self.dynamic[number]=result
        return result
Published 160 original articles · won praise 30 · views 70000 +

Guess you like

Origin blog.csdn.net/yixieling4397/article/details/104876286