Climbing stairs "Leetcode" recursion, dynamic programming, Java, algorithm

Source: Internet
Author: User

You is climbing a stair case. It takes n steps to reach the top.

Each time you can either climb 1 or 2 steps. In what many distinct ways can you climb to the top?

Note:given N would be a positive integer.

Topic Analysis: Each time can only walk 1 or 2 steps, ask n steps how many in the way????

You can use dynamic planning and recursive resolution, the process of submitting code may occur when n is too large, time-out hints, this time we have to consider the complexity of the time.

Pre-Knowledge: What is the relationship between recursion and dynamic planning and the divide-and-conquer approach?

    1. Dynamic planning implementation: both time and space complexity are Max O (n), code and run time as

 Public classSolution { Public intClimbstairs (intN) {if(n = = 1) {            return1; }        int[] DP =New int[n + 1]; dp[1] = 1; dp[2] = 2;  for(inti = 3; I <= N; i++) {Dp[i]= Dp[i-1] + dp[i-2]; }        returnDp[n]; }}      

Fibonacci NumberThe algorithm time is O (n) space is O (1)

 Public classSolution { Public intClimbstairs (intN) {if(n==1){            return1; }        intFirst=1; intsecond=2;  for(inti=3;i<=n;i++){            //F (3) =f (2) +f (1); //F (4) = (F3) +f (2);            intThird =0; Third=first+second; First=second; Second=third; }        returnsecond; }}

This gives the running time

Recursive method implementation ()

 Public classSolution { Public intClimbstairs (intN) {intMem[] =New int[N+1]; returnClimb (0, N,mem); }     Public intClimbintIintNintMem []) {                if(i>N) {            return0; }        if(i==N) {            return1; }        if(mem[i]>0){            returnMem[i]; } Mem[i]=climb (I+1,N,MEM) +climb (i+2, N,mem); returnMem[i]; }}

1. Divide and conquer strategy (Divide and Conquer)

The original problem is decomposed into several small but similar sub-problems (Divide), solving these sub-problems recursively (conquer), and then merging the solutions of these sub-problems to establish the solution of the original problem. Because in solving large problems, it is necessary to solve the problem of recursion, so it is usually implemented by recursive method , that is, from top to bottom.

2. Dynamic Programming (programming)

The dynamic programming is similar to the divide-and-conquer strategy, and it also decomposes the original problem into several smaller sub-problems, solves these sub-problems recursively, and then merges the solutions of the sub-problems to get the solution of the original problem. The difference is that these sub-problems will overlap, a sub-problem after the solution, it may be solved again, so we think of these sub-problems to save the solution, when the next time to solve this sub-problem, directly take it. In fact, the problem solved by dynamic programming is a subset of the problem solved by the divide-and-conquer strategy, but this subset is more suitable to be solved by dynamic programming to get smaller running time. the problem that can be solved by dynamic programming is sure to be solved, but the running time is long. Therefore, the divide-and-conquer strategy is generally used to solve the problem of opposing sub-problems, called Standard division, and dynamic programming is used to solve the problem of overlapping of sub-problems .

Dynamic programming is generally implemented by two methods, a top-down memo method, recursive implementation, a bottom-up approach, and iterative implementation.

3, greedy algorithm (greedy algorithm)

The greedy algorithm makes the optimal choice at every step, hoping that this choice can lead to the global optimal solution. Yes, just hope, so the greedy algorithm is not guaranteed to get the optimal solution, but it does get the best solution for many problems, and the running time is shorter. Thus, the greedy algorithm is an heuristic algorithm. When can we use the greedy algorithm? When the problem has the greedy choice nature, we can use the greedy algorithm to solve the problem.
Greedy Choice Nature: We can construct the global optimal by making local optimal (greedy). as long as we can prove that the problem has the greedy choice nature, it can be solved by greedy algorithm. For example, for the 0-1 knapsack problem, we may not get the optimal solution with the greedy algorithm (of course, may also get the optimal solution), but for some knapsack problem, we can get the optimal solution, the greedy algorithm can be used as an approximate algorithm for the 0-1 knapsack problem.

A comparison between dynamic programming and recursion

In terms of performance, I use recursive and dynamic programming to achieve the Fibonacci sequence calculation, recursion if more than 40 time has taken a long time, 40 times about 1 seconds, but with dynamic planning to 100 million times, only need 4 seconds, this difference is not a few orders of magnitude problem. In fact, the recursive implementation of the Fibonacci sequence computation time complexity is O (2?), the dynamic planning to achieve the time complexity of O (n) So, in the future development, try to avoid the use of recursion.
In terms of specific implementations, dynamic programming is only one step in the operation of saving sub-problem calculation results than normal recursion.
For example, the recursive implementation of the Fibonacci sequence is as follows:

int F (int  i)    {             if(i < 1)  return 0;              if return 1;               return F (i-1) + f (i-2);    }

    • The implementation of the dynamic programming algorithm is this:

int F (int  i) {     if(knownf[i]! = unknown)        {return  knownf[i];     }      if (i = = 0) T =0     ; if (i = = 1) t =1     ; if (i > 1)  T = f (i-1) + f (i-2);      return Knownf[i] = t;}

4. Summary

    1. The divide-and-conquer strategy is used to solve the problem of the structure similarity between the original problem and sub-problem, and the recursive implementation is generally used for each sub-problem.
    2. The dynamic programming is used to solve the problem of sub-problems, which can be implemented either recursively or iteratively.
    3. Greedy algorithm is used to solve a kind of problem with greedy choice, which can be realized by recursion or iterative, because many recursive greedy algorithms are tail recursion, so it is easy to change to iterative greedy algorithm;
    4. Recursion is the means of realization, the strategy of divide and conquer is the thought of solving the problem, and the dynamic programming often uses the recursive realization of the result of recording sub-problem.

Resources:
1.http://1661518.blog.51cto.com/1651518/1396943
2. The third edition of the introduction to algorithms

3.http://blog.csdn.net/tyhj_sf/article/details/53969072

Climbing stairs "Leetcode" recursion, dynamic programming, Java, algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.