[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 ...
随机推荐
- 手把手带你使用JS-SDK自定义微信分享效果
https://www.cnblogs.com/backtozero/p/7064247.html
- Concept of function continuity in topology
Understanding of continuity definition in topology When we learn calculus in university as freshmen, ...
- 项目必备!永无 bug 注释
佛祖保佑 永无bug 代码注释 // // _oo0oo_ // o8888888o // 88" . "88 // (| -_- |) // 0\ = /0 // ___/`-- ...
- 【Codeforces】【网络流】【线段树】【扫描线】Oleg and chess (CodeForces - 793G)
题意: 给定一个n*n的矩阵,一个格子上可以放一个车.其中有q个子矩阵,且q个子矩阵互不相交或者是重叠(但边界可以衔接).这q个子矩阵所覆盖的地方都是不能够放车的.车可以越过子矩阵覆盖的地方进行攻击( ...
- 使用boost.python封装C++库
使用boost.python封装C++库 C++以高性能著称,但是编写较为复杂.而简洁是Python的强项.如果能珠联璧合,就能发挥两家之长.本文尝试用boost库的python模块封装C++ 前期准 ...
- centos7-- sbt的安装使用
sbt是一款类似于maven的构建工具 安装sbt curl https://bintray.com/sbt/rpm/rpm > bintray-sbt-rpm.repo mv bintray- ...
- Java 前端模板引擎学习:thymeleaf 模板引擎
模板引擎接口 ITemplateEngine 一.后台数据与外部数据 1.处理后台数据 $表达式是个变量表达式,用于处理在 request parameters and the request, s ...
- Oracle 备份与恢复
在进行生产服务器升级.或更换数据库服务器.搭建测试环境时,需要对生产数据库进行备份以及将来可能的还原. 1.expdp导出 expdp DMS version directory=DATA_PUMP_ ...
- 微信小程序里的bug---video 的play()
微信小程序hidden转换后执行play()用真机测试不会播放.在调试器里可以. 解决方法,把hidden换成wx:if. 我刚开始以为网速问题,其实不是, 具体我也不知道为什,换上wxif解决了.
- 图片上传 new FormData() ,new FileReader()
多图片和单图片取决于 multiple属性,下面来介绍下 new FileReader() reader.readAsDataUrl(file[0]) 可以看到文件是Base64的, let fd = ...