题目:

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.

Approach #1 Brute Force [Time Limit Exceeded]

public class Solution {
public int climbStairs(int n) {
climb_Stairs(0, n);
}
public int climb_Stairs(int i, int n) {
if (i > n) {
return 0;
}
if (i == n) {
return 1;
}
return climb_Stairs(i + 1, n) + climb_Stairs(i + 2, n);
}
}

Time complexity : O(2^n). Size of recursion tree will be 2^n​​.

Space complexity : O(n). The depth of the recursion tree can go upto n.

Approach #2 Recursion with memorization [Accepted]

public class Solution {
public int climbStairs(int n) {
int memo[] = new int[n + 1];
return climb_Stairs(0, n, memo);
}
public int climb_Stairs(int i, int n, int memo[]) {
if (i > n) {
return 0;
}
if (i == n) {
return 1;
}
if (memo[i] > 0) {
return memo[i];
}
memo[i] = climb_Stairs(i + 1, n, memo) + climb_Stairs(i + 2, n, memo);
return memo[i];
}
}

Time complexity : O(n). Size of recursion tree can go upto n.

Space complexity : O(n). The depth of recursion tree can go upto n.

Approach #3 Dynamic Programming [Accepted]

public class Solution {
public int climbStairs(int n) {
if (n == 1) {
return 1;
}
int[] dp = new int[n + 1];
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
}

Time complexity : O(n). Single loop upto n.

Space complexity : O(n). dp array of size n is used.

Approach #4 Fibonacci Number [Accepted]:

public class Solution {
public int climbStairs(int n) {
if (n == 1) {
return 1;
}
int first = 1;
int second = 2;
for (int i = 3; i <= n; i++) {
int third = first + second;
first = second;
second = third;
}
return second;
}
}

Time complexity : O(n). Single loop upto n is required to calculate n^{th} fibonacci number.

Space complexity : O(1). Constant space is used.

原文:https://leetcode.com/articles/climbing-stairs/

算法题之Climbing Stairs(leetcode 70)的更多相关文章

  1. Min Cost Climbing Stairs - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Min Cost Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题也是一道dp题.dp[i]表示爬到第i层 ...

  2. Climbing Stairs - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题是一题非常经典的DP题(拥有非常明显的重叠子结构).爬到n ...

  3. Cllimbing Stairs [LeetCode 70]

    1- 问题描述 You are climbing a stair case. It takes n steps to reach to the top. Each time you can eithe ...

  4. [面试算法题]比较二叉树异同-leetcode学习之旅(5)

    问题描述 Given two binary trees, write a function to check if they are equal or not. Two binary trees ar ...

  5. climbing stairs leetcode java

    问题描述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...

  6. [每日一题2020.06.14]leetcode #70 爬楼梯 斐波那契数列 记忆化搜索 递推通项公式

    题目链接 题意 : 求斐波那契数列第n项 很简单一道题, 写它是因为想水一篇博客 勾起了我的回忆 首先, 求斐波那契数列, 一定 不 要 用 递归 ! 依稀记得当年校赛, 我在第一题交了20发超时, ...

  7. LeetCode算法题-Climbing Stairs(Java实现)

    这是悦乐书的第159次更新,第161篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第18题(顺位题号是70).你正在爬楼梯,它需要n步才能达到顶峰.每次你可以爬1或2步, ...

  8. LeetCode算法题-Min Cost Climbing Stairs(Java实现)

    这是悦乐书的第307次更新,第327篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第176题(顺位题号是746).在楼梯上,第i步有一些非负成本成本[i]分配(0索引). ...

  9. LeetCode练题——70. Climbing Stairs

    1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...

随机推荐

  1. django类视图简单使用和源码解析

    django的类视图,CBV: 我们在开始接触django的时候,习惯于使用函数编写视图,即FBV.使用FBV时,我们只需要在路由匹配时,对应的路由下找到这个函数就可以了,这样做看似很和谐,但是有的时 ...

  2. mysql数据库,编码错误解决

    在写代码的过程中,经常会遇见,将中文字符输入到mysql数据库中,但是查看的时候,却发现,中文显示为乱码的情况,让人相当的头疼,今天正好解决了一个这样遇到的问题,所以简单总结一下: 1.首先查看数据库 ...

  3. 【iOS开发】动态添加子视图 UIView 的正确方法

    很多时候哥比较喜欢用代码添加视图,特别是要同时加很多UIView时,而且跟 xib 比起来代码更容易管理,在多人的项目中代码不容易 conflict. 但小牛哥最近发现很多新人都不太清楚正确的使用方法 ...

  4. 【积累】LinqToSql复合查询结果转DataTable数据

    最近的项目用到了大量的复合查询结果用于数据源,绑定到数据控件上. 为了方便,我们把它转换成DataTable的数据源形式.请看下面的示例: 1)思考自己需要的数据,然后组合,因此创建一个新的类: // ...

  5. PHP+AJAX 实现表格实时编辑

    https://blog.csdn.net/qq_29627497/article/details/81365107 源码链接:https://pan.baidu.com/s/1fAinVXU-nWt ...

  6. CSS兼容性总结

    一.针对IE6的 !important 必须写在前面,例如: background:#9C6 !important;background:#999; 二.CSS HACK //IE6 专用 _heig ...

  7. 在.net2.0下使用System.Web.Script.Serialization;

    最近,在弄json字符串转为对象.需要添加这个引用System.Web.Script.Serialization;因为版本必须是dotnet2.0的原因,发现很多解决方案不适合自己.故使用这种解决办法 ...

  8. P2874 [USACO07FEB]新牛棚Building A New Barn

    题目描述 After scrimping and saving for years, Farmer John has decided to build a new barn. He wants the ...

  9. 根据约束id名找到表名

    数据库运行删除某条数据出现 [Err] ORA-02292: 违反完整约束条件 (ITOUCH_FDA.FKC7DB45E29C5A81ED) - 已找到子集 根据ITOUCH_FDA.FKC7DB4 ...

  10. inflate

    LayoutInflater是用 来找res/layout/下的xml布局文件,并且实例化 https://www.cnblogs.com/savagemorgan/p/3865831.html