LeetCode of JavaScript answer the first 70 questions - climbing stairs (Climbing Stairs)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_36903042/article/details/89292992

Time:2019/4/12
Title:Clibing Srairs
Difficulty: Easy
Author:小鹿


Topic: Climbing Stairs

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

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

Note: Given n will be a positive integer.

Suppose you are climbing stairs. You need n order to reach your roof.

Every time you can climb one or two steps. How many different ways can climb to the roof of it?

Note : Given n is a positive integer.

Example 1:

Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:

Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

the word

▉ algorithm ideas

Two kinds Solutions, the first recursive; second dynamic programming.

1) a recursive implementation:

First of all, we need to know three conditions to be satisfied by the use of recursion, we had previously talked about the kinds of questions in front of, behind organized into a series of articles I will be easy for everyone to learn. Then follow the way we have said before to write recursive formula, and then converted into a recursive code. We will find recursive time complexity is O (2 ^ n), if we remember that there is a recursive drawback is wary of repeating elements recursive calculation. Because the calculation is repeated with the elements, resulting in a time complexity into exponential growth.

In order to reduce the complexity of the time, we can use a hash table to record the values ​​recorded over repeating elements, but need to apply additional space for storage, resulting in the spatial complexity is O (n), reduced time complexity of O (n), also it took advantage of the idea of ​​space for time.

2) implementation of Dynamic Programming:

We can find carefully recursive mode or above can be optimized, and we another way to think, to think from the bottom up, in fact, we calculate two values ​​before storing a value on it (for example: the calculation f (6) need to know the value f (5) and f (4) on it), we can not value before storage, this time can be reduced to the space complexity O (1).

▉ code implements (recursively)

Recursive optimized.

//递归实现
//时间复杂度为 O(n),空间复杂度为 O(n)
var climbStairs = function(n) {
    let map = new Map();
    if(n === 1) return 1;
    if(n === 2) return 2;
    if(map.has(n)){
    	return map.get(n);
    }else{
        let value = climbStairs(n - 1) +climbStairs(n - 2);
        map.set(n,value);
        return value;
    }
};

▉ code implementation (dynamic programming)

//动态规划
//时间复杂度为O(n) 空间复杂度为O(1)
var climbStairs = function(n) {
    if(n < 1) return 0;
    if(n === 1) return 1;
    if(n === 2) return 2;

    let a = 1;
    let b = 2;
    let temp = 0;

    for (let i = 3; i < n + 1; i++) {
        temp = a + b;
        a = b;
        b = temp;          
    }
    return temp;
}

No. I personally welcome attention to the public: "a code unwilling to ordinary farmers," all the way to record the story of his own self-programming.
LeetCode resolve other topics, Github: https://github.com/luxiangqiang/JS-LeetCode

Guess you like

Origin blog.csdn.net/qq_36903042/article/details/89292992