The sword refers to offer 07-10 Dynamic programming problem solving method for Fibonacci series type problems (recursive method)

I took a cursory look at the previews of the blog articles, and there are not many reasonable ones, but generally speaking, the question in the Ali written test is more eye-catching than other questions. By the way, the NetEase written test is coming tomorrow night, and it is my first official written test, so although I don’t hold out hope, I still have to struggle twice. So today, hurry up and brush up on two more questions. . . Just kidding, it's just a matter of routine.
07 Fibonacci sequence (top-down approach)
It stands to reason that the most intuitive way to solve the problem of the Fibonacci sequence is recursion, that is, returning the sum of its n-1 and n-2 functions themselves, but in fact, when it comes to the Fibonacci sequence, we have to mention a famous one Solution: Dynamic programming. So people who look at the algorithm for the first time may be as frightened by this name as I am, as if it is very powerful. It is really difficult, but it is difficult to understand, in fact, this method can greatly reduce the time complexity of the code. The Fibonacci sequence happens to be a problem that dynamic programming can optimize the computing time of recursive algorithms.
The use of dynamic programming has the same characteristics and can divide a problem into several smaller problems to solve, and each sub-problem can be subdivided, so the problem becomes more and more subdivided. There are also top-down and bottom-up solutions. A top-down approach uses a table to record half-way values ​​to reduce recalculation time. Most of the hard difficulty problems involve this algorithm. Of course, this is a brief mention here. If you want to taste it carefully, you still need an introduction to algorithms, but to be honest, I read the dynamic programming chapter in the introduction to algorithms and always felt a little confused. Understand. . . It may be necessary to cooperate with the topic to better understand the algorithm. Well, specific to the topic, the main rule of the Fibonacci sequence is that the nth element is the combination of its n-1 and n-2th elements, that is, F(3)=F(2)+F(1) ........;So the question asks for the nth item, then the first n-1 and n-2 items are needed to determine the value of the nth item. Therefore, by establishing two numbers and continuously recording the n-1 and n-2 items to the n-th item, the solution to the problem can be obtained. code show as below:
class Solution {
public:
    int Fibonacci(int n) {
        int result[2]={0,1};
        if(n<2)
            return result[n];
        long long maxNum=1;
        long long minNum = 0;
        long long answer=0;
        for(int i=2;i<=n;i++){
            answer = maxNum + minNum;
            minNum = maxNum;
            maxNum=answer;
        }
        return answer;
    }
};

Pay attention to control the return value when n<2, and do not write int result[2]={0,1}; as int result[2]={'0','1'} when defining elements ; will directly cause the computer to judge it as a character type, so when the input is 0, its output is 48; students who want to see the recursive method can refer to the recursive solution of question 08
08 Frog jumping steps

This question is generally similar to the previous question, that is, the frog may jump 1 or jump 2, that is, the two jump methods first write a recursive operation:
class Solution {
public:
    int jumpFloor(int number) {
            if(number<=0)
                return 0;
        if(number==1)
            return 1;
        if(number==2)
            return 2;
        return jumpFloor(number-1)+jumpFloor(number-2);
    }
};
Mainly because I am not very proficient with recursion, I plan to practice my hands, and then formally write the following code:
    int jumpFloor(int number) {
        if(number==0)
            return 0;
        if(number==1)
            return 1;
        if(number==2);
        return 2;
        int f1=1,f2=2,answer=0;
        for(int i=3;i<=number;i++){
            answer=f1+f2;
            f1=f2;
            f2=answer;
        }
        return answer;
    }
Then it crashed, stuck in the place where number==3 and will not enter the loop, the direct output is 2, which is not very clear at all, so open the compiler layer by layer and finally find out what's wrong. The problem is not big, but it is often fatal, so the modified code is as follows: (Interested students can compare and see what is wrong)
class Solution {
public:
    int jumpFloor(int number) {
        if(number==0)
            return 0;
        if(number==1)
            return 1;
        if(number==2)
            return 2;
        int f1=1,f2=2,answer=0;
        for(int i=3;i<=number;i++){
            answer=f1+f2;
            f1=f2;
            f2=answer;
        }
        return answer;
    }
};

09 Perverted Frog Jump
Since there are n possibilities for a metamorphic frog to jump, the expressions for different possibilities are: F(n)=F(n-1)+F(n-2)+...+F(2)+F(1 )+F(0);
                                                                                              F(n-1)=F(n-2)+F(n-3)+...+F(1)+F(0);
So according to the above formula, it can be deduced that F(n)=2*F(n-1); that is, write the following code:
class Solution {
public:
    int jumpFloorII(int number) {
        if(number<=0)
            return -1;
        if(number==1)
            return 1;
        else
            return 2*jumpFloorII(number-1);
    }
};

10 Matrix Coverage
On the surface, it seems that this problem has nothing to do with the Fibonacci sequence, but in fact it is still a Fibonacci sequence problem, that is, the solution of the nth item is the sum of the previous two items. For example, a 2*3 matrix can be dynamically The planning method decomposes it into a 1*2 and a 2*2 square, and 1*2 is the solution when n==1, and 2*2 is the solution when n==2. So for F(3)=F(2)+F(1) when n==3; and when n==4, there are two solutions: vertical and horizontal, and the rest are 2*3 and 2 *2 two rectangles, so F(4)=F(3)+F(2);. After getting the rule, the code implementation is the same as the first question:
class Solution {
public:
    int rectCover(int number) {
        if(number==0)
            return 0;
        if(number==1)
            return 1;
        if(number==2)
            return 2;
        int f1=1,f2=2,answer=0;
        for(int i=3;i<=number;i++){
            answer=f1+f2;
            f1=f2;
            f2=answer;
        }
        return answer;
    }
};
Well, in the end, I won't say anything more, just take a sip of my own milk: I feel that NetEase will be gone tomorrow. . . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325728690&siteId=291194637