算法题之Climbing Stairs(leetcode 70)
题目:
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)的更多相关文章
- Min Cost Climbing Stairs - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Min Cost Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题也是一道dp题.dp[i]表示爬到第i层 ...
- Climbing Stairs - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题是一题非常经典的DP题(拥有非常明显的重叠子结构).爬到n ...
- 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 ...
- [面试算法题]比较二叉树异同-leetcode学习之旅(5)
问题描述 Given two binary trees, write a function to check if they are equal or not. Two binary trees ar ...
- climbing stairs leetcode java
问题描述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...
- [每日一题2020.06.14]leetcode #70 爬楼梯 斐波那契数列 记忆化搜索 递推通项公式
题目链接 题意 : 求斐波那契数列第n项 很简单一道题, 写它是因为想水一篇博客 勾起了我的回忆 首先, 求斐波那契数列, 一定 不 要 用 递归 ! 依稀记得当年校赛, 我在第一题交了20发超时, ...
- LeetCode算法题-Climbing Stairs(Java实现)
这是悦乐书的第159次更新,第161篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第18题(顺位题号是70).你正在爬楼梯,它需要n步才能达到顶峰.每次你可以爬1或2步, ...
- LeetCode算法题-Min Cost Climbing Stairs(Java实现)
这是悦乐书的第307次更新,第327篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第176题(顺位题号是746).在楼梯上,第i步有一些非负成本成本[i]分配(0索引). ...
- LeetCode练题——70. Climbing Stairs
1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...
随机推荐
- JVM 什么时候会触发FGC
1:System.gc(); 2:老年代满了 没啥好说的从年轻代去往老年代的 3:JDK7或JDK6中永久区满了 得看是否还会有分配,如果没有就不会进行FGC,不过CMS GC下会看到不停地CMS G ...
- coreos install hpssacli
基于官方的coreos ramdisk安装hp raid管理工具,其版本为debian8 apt-get install curl nano /etc/apt/sources.list deb htt ...
- remix无法安装的解决方案
无法安装的原因: 因为remix依赖python 执行python又依赖c++的环境 所以连环导致出错 https://github.com/nodejs/node-gyp 措施一:降级处理 先清理缓 ...
- BZOJ 2049 SDOI2008 洞穴勘测 LCT板子
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 题意概述:给出N个点,一开始不连通,M次操作,删边加边,保证图是一个森林,询问两点连 ...
- HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- winform自动最大化(在不同分辨率情况下)
load函数末尾加: System.Drawing.Rectangle rec = Screen.GetWorkingArea(this); int SH = rec.Height; int SW = ...
- vim字符编码
今天我在用vim新建中文文件的时候遇到保存好出现乱码的问题,经过一波百度, :set encoding=utf-8 :set fileencodings=ucs-bom,utf-8,cp936 :se ...
- [翻译] ASP.NET Core 简介
ASP.NET Core 简介 原文地址:Introduction to ASP.NET Core 译文地址:asp.net core 简介 翻译:ganqiyin ...
- Spring MVC前台POST/GET方式传参数的方法
假设前台通过submit传值,代码如下: <form action="testPost.do" method="post"> 页码:<inpu ...
- iOS-SDWebImage的原理以及使用流程
SDWebImage 支持异步的图片下载+缓存,提供了 UIImageView+WebCacha 的 category,方便使用.SDWebImage加载图片的流程: 1. 入口 setImageWi ...