[LeetCode] Climbing Stairs (Sequence DP)
Climbing Stairs
https://oj.leetcode.com/problems/climbing-stairs/
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?
这题比较简单,可以使用动态规划来求解。请看以下分析:
State:f[i] 表示从起点出发达到第 i 个位置的方案总数
Function:由于第 i 个位置可以由第 i – 2 个位置走两步或者由第 i – 1 个位置走一步而到达,因此有以下状态转移方程:
f[i] = f[i-1] + f[i-2]
Initialize:1. 从起点走到第一个位置,显然只有走 1 步到达这一种方案。
2. 从起点走到第二个位置,有两种方案:直接走 2 步或者每次走 1 步,走 2 次。因此,初始化状态如下:
f[0] = 1
f[1] = 2
注意:数组下标从0开始。
Answer:f[n - 1] 。
下面为 AC 的代码:
/**
* Author : Zhou J
* Email : zhoujx0219@163.com
*/ class Solution {
public:
int climbStairs(int n)
{
if (n == 0)
{
return 0;
} // State: 从起点走到第 i 个位置的方案总数
int sum[n]; // initialize
sum[0] = 1;
if (n >= 2)
{
sum[1] = 2;
} // switch the state
for (size_t ix = 2; ix < n; ++ix)
{
sum[ix] = sum[ix - 1] + sum[ix - 2];
} return sum[n - 1];
}
};
Optimize
当然,此处并不需要使用一个 n 维的数组来存放 State ,观察状态转移方程就可以知道,此处只需要两个变量来存放状态即可。因此下面的代码对空间做了进一步的优化:
/**
* Author : Zhou J
* Email : zhoujx0219@163.com
*/ class Solution {
public:
int climbStairs(int n)
{
if (n <= 2)
{
return n;
} size_t now;
size_t lastlast = 2; // f[1]
size_t last = 1; // f[0] // switch the state
for (size_t ix = 2; ix < n; ++ix)
{
now = lastlast + last;
last = lastlast;
lastlast = now;
} return now;
}
};
[LeetCode] Climbing Stairs (Sequence DP)的更多相关文章
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- LeetCode算法题-Min Cost Climbing Stairs(Java实现)
这是悦乐书的第307次更新,第327篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第176题(顺位题号是746).在楼梯上,第i步有一些非负成本成本[i]分配(0索引). ...
- LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)
翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 You are climbing a stair case. It tak ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
- LeetCode算法题-Climbing Stairs(Java实现)
这是悦乐书的第159次更新,第161篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第18题(顺位题号是70).你正在爬楼梯,它需要n步才能达到顶峰.每次你可以爬1或2步, ...
- LeetCode Unique Paths (简单DP)
题意: 给出一个m*n的矩阵,robot要从[1][1]走到[m][n],每次只能往下/右走,问有多少种走法? 思路: DP的经典问题.先将[1][1]设为1,然后两种走法就是分别用[i][j]去更新 ...
- 力扣—climbing stairs(爬楼梯) python实现
题目描述: 中文: 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 英文: You are cl ...
- leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution)
leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution) On a staircase, the i-th step ...
- Bomb HDU - 3555 (数位DP)
Bomb HDU - 3555 (数位DP) The counter-terrorists found a time bomb in the dust. But this time the terro ...
随机推荐
- 第六章 图(a)概述
- Android logcat输出中文乱码
使用adb的logcat 命令查看系统日志缓冲区的内容,会发现在CMD的界面面,直接输出的中文内容是乱码. 这个问题出现在使用logcat将日志直接打印在当前的DOS窗口的时候会出现:使用logcat ...
- 使用Spring+Junit4进行测试
前言 单元测试是一个程序员必备的技能,我在这里就不多说了,直接就写相应的代码吧. 单元测试基础类 import org.junit.runner.RunWith; import org.springf ...
- win 下 apache 虚拟主机配置方式
虚拟主机的配置在apache安装目录下/conf/extra/httpd-vhosts.conf文件中,需要在/conf/httpd.conf中开启. LoadModule vhost_alias_m ...
- 欲哭无泪的p-value = 0.051 | 做几次重复能得到较低的p-value
欲哭无泪的p-value = 0.051 | 做几次重复能得到较低的p-value 已有 1469 次阅读 2017-12-15 14:12 |个人分类:RNA-seq|系统分类:科普集锦|关键词:R ...
- [NOI.AC]DELETE(LIS)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRMAAASJCAYAAABLtYu4AAAgAElEQVR4Xuzdf2xTd74n/PeqI/NsNB ...
- Vue.js 与 Laravel 分离
首先表示折腾了十来天的php-laravel框架和vue的结合开发又偏前端实在是太大的阻碍,首先laravel的机制就是写完路由router再加载blade模板的.如果要在laravel工程里面加载一 ...
- tomcat 启动报 找不到 StrutsPrepareAndExecuteFilter。。
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://w ...
- spring 3 mvc 的 @ResponseBody返回数据中文乱码
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter&q ...
- 库函数方式文件编程----fopen
使用fopen等文件访问库函数编写应用程序,该应用程序实现文件的复制功能. 1.打开文件---fopen 函数功能:打开文件 头文件:#include<stdio.h> 函数原型:FILE ...