Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【算法-动态规划-简单】爬楼梯 #89

Open
AwesomeDevin opened this issue May 24, 2023 · 0 comments
Open

【算法-动态规划-简单】爬楼梯 #89

AwesomeDevin opened this issue May 24, 2023 · 0 comments

Comments

@AwesomeDevin
Copy link
Owner

AwesomeDevin commented May 24, 2023

https://leetcode.cn/problems/climbing-stairs/solution/pa-lou-ti-by-leetcode-solution/

缓存递归

时间复杂度 O(n)
空间复杂度O(n)

var climbStairs = function(n) {

    const cache = {}
    const fn = (n) => {
        if(n === 1) return 1
        if(n === 2) return 2
        if(cache[n]) return cache[n]
        const res = fn(n-1) + fn(n-2) 
        cache[n] = res
        return res
    }
    return fn
}();

动态规划(滚动数组)

时间复杂度 O(n)
空间复杂度O(1)

var climbStairs = function(n) {
    let a = 0 
    let b = 0
    let c = 1
    for(let i =0;i <n; i++ ){
        b = a
        a = c
        c = a + b
    }
    return c
};

动态规划

@AwesomeDevin AwesomeDevin changed the title 【算法-动态规划】爬楼梯 【算法-动态规划-简单】爬楼梯 May 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant