[LeetCode] 70. Climbing Stairs_ Easy tag: Dynamic Programming
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.
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 这个题目就是找到方程 f(i) = f(i - 1) + f(i - 2), i >= 2,然后用dynamic programming, 不过可以用滚动数组去优化。S:O(1) Code
class Solution:
def climbStairs(self, n):
mem = list(range(1, n + 1)) # in python3 , range is an iterator
for i in range(2, n):
mem[i] = mem[i - 1] + mem[i - 2]
return mem[n - 1]
滚动数组
class Solution:
def climbStairs(self, n):
mem = list(range(1, 4)) # in python3 , range is an iterator
for i in range(2, n):
mem[i%3] = mem[i%3 - 1] + mem[i%3 - 2]
return mem[(n - 1)%3]
滚动数组2
class Solution:
def climbStairs(self, n):
dp = [1, 2]
for i in range(2, n):
dp[i %2] = dp[(i - 1)%2] + dp[(i - 2)%2]
return dp[(n - 1)%2]
[LeetCode] 70. Climbing Stairs_ Easy tag: Dynamic Programming的更多相关文章
- 70. Climbing Stairs(easy, 号称 Dynamic Programming 天下第一题)
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...
- [LeetCode] 115. Distinct Subsequences_ Hard tag: Dynamic Programming
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- [LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [LeetCode] 198. House Robber _Easy tag: Dynamic Programming
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
随机推荐
- EChart 文字大小调整 饼状图为例
一.EChart图中的文字调整(以饼图为例) 二.源码: { "title": { "text": "", "subtext&qu ...
- numpy的基础运算2-【老鱼学numpy】
numpy的基础运算中还有很多运算,我们这里再记录一些. 最小/大值索引 前面一篇博文中我们讲述过如何获得数组中的最小值,这里我们获得最小/大值的索引值,也就是这个最小/大值在整个数组中位于第几位. ...
- skywalking6.0.0安装配置(windows),以mysql作为储存。
下载skywalking6.0.0http://skywalking.apache.org/downloads/ 下载jdk8https://www.oracle.com/technetwork/ja ...
- 新世界主机_XenServer7.0都有哪些优势?
新世界主机VPS全部都采用了Xen硬件虚拟化技术,每个用户都能够独享资源,一键就可以创建和重装VPS,每个VPS都拥有足够的带宽,保证顺畅运行(http://m.0830mn.com). 新世界主机使 ...
- [转自大神]js拖拽小总结
https://blog.csdn.net/u013040887/article/details/83059094 权侵删 这里写的是一个原生js实现拖拽的效果,首先: 1.实现拖拽的三大事件,是要首 ...
- 初识Python,简单初学代码
第一个自己手写的代码~ If 与 Elif #!/usr/bin/env python # - * - coding:uft8 - * - Inp = input ( '请输入你的会员级别' ) if ...
- cache 缓存的处理
, ) { ? ? ) && ? lstorage.remove(key) : sstorage.remove(key) } /** * 更新缓存配置文件 */ Cache.proto ...
- webpack报错需要合适的loader
以前做vue项目都好好的,最近做react,公共配置感觉加个jsx就可以了吧,然而不是这样的. 一.问题描述 You may need an appropriate loader to handle ...
- Visual GC提示"不受此JVM支持“解决方案(配置jstatd)
Visual GC提示"不受此JVM支持“,如果想使用这个插件,就需要配置jstatd连接方式,下面来看jstatd的配置: 1.配置安全策略文件路径$JAVA_HOME/jre/lib/s ...
- 执行Python出现LookupError: unknown encoding: cp65001解决办法
执行Python出现LookupError: unknown encoding: cp65001错误 dos下执行以下命令即可 chcp 以上.