[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 ...
随机推荐
- SpringBoot之hello world!
哈哈哈,还是在元旦这一天对你下手了.麻溜的给自己充电,在这个寒冬,不断听到裁员的消息或者新闻,可对于我这个外包和外派的人来说,好像并没有受到什么影响.可能是人手不够可能是项目很忙.对于明年的三月金四月 ...
- python 代理
1.参考 http://docs.python-requests.org/en/master/user/advanced/ Using Python’s urllib2 or Requests wit ...
- manjaro安装及设置
因我的笔记本(联想的拯救者)昨晚打开后什么都还没做就被更新系统“抢走”了画面导致按什么都不管用 所以就想起能不能不用win系统,都知道linux比win稳定,so....就找到了manjaro.以下是 ...
- 如何用div绘制一个容器,例如圆形
需求:一个圆形的容器,里面有两种颜色,随比率不同而变化. 实现同一个容器中两种颜色变化的关键是css渐变 //background:linear-gradient(#818181 30%,#06FF3 ...
- 【Codeforces】【图论】【数量】【哈密顿路径】Fake bullions (CodeForces - 804F)
题意 有n个黑帮(gang),每个黑帮有siz[i]个人,黑帮与黑帮之间有有向边,并形成了一个竞赛完全图(即去除方向后正好为一个无向完全图).在很多年前,有一些人参与了一次大型抢劫,参与抢劫的人都获得 ...
- [BZOJ2457][BeiJing2011]双端队列 (单调性)
正如lyd所说,和数据结构本身没什么太大关联 题意 中文题面 Sherry现在碰到了一个棘手的问题,有N个整数需要排序. Sherry手头能用的工具就是若干个双端队列. ...
- bat入门--第一个bat文件
所谓的批处理就是从记事本开始进行的. 1.新建一个记事本文件, 2, 打开的记事本上敲入一行字:@echo off 意思:隐藏以下输入的代码(off改成on是打开代码显示). 3.再输入:echo h ...
- swift中Cell的内容定制
1.cellForTitle 2.register
- HTML5_canvas_填充文本,描边文本
canvas 文本相关 填充文本 pen.fillText("HelloWorld", 100, 100); 文本的(100, 100) 在文本基线处 文本样式 pe ...
- (18)0907_CSS选择器详解
选择器的优先级(决定那个样式生效): important: > 内联样式 > id选择器> 类和伪类 > 元素选择器 > 通配选择器> 继承样式无优先级 最大 ...