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?
现在说一下大致思路:求出递推公式
f(n)=f(n-1)+f(n-2) ===>f(n)+f(n-1)=2f(n-1)+f(n-2)
[f(n) f(n-1)]=[[1 1][1 0]][f(n-1) f(n-2)]
可以得到递推矩阵
所以该算法的关键点就是:1.需要求出递推矩阵;2.写一个方法,能够实现矩阵相乘
虽然代码量会比其他几个方法大,但是算法复杂度比较低
* 动态规划解法
*/
public int climbStairs3(int n) {
if (n <= )
return ;
if (n == )
return ;
if (n == )
return ; int[][] base = { { , }, { , } };
int[][] res = matrixPower(base, n - ); return *res[][] + res[][];
}
/*
* 两个矩阵相乘
*/
private int[][] muliMatrix(int[][] m1, int[][] m2) {
int[][] res = new int[m1.length][m2[].length];
for (int i = ; i < m1.length; i++) {
for (int j = ; j < m2[].length; j++) {
for (int k = ; k < m2.length; k++) {
res[i][j] += m1[i][k] * m2[k][j];
}
}
}
return res;
}
包含三种最常用的回答,第一最优,第三最差
* 空间复杂度O();
*/
public int climbStairs(int n) {
if (n < )
return n;
int one_step_before = ;
int two_steps_before = ;
int all_ways = ;
for (int i = ; i < n; i++) {
all_ways = one_step_before + two_steps_before;
two_steps_before = one_step_before;
one_step_before = all_ways;
}
return all_ways;
}
/*
* 空间复杂度O(n);
*/
public int climbStairs_2(int n) {
if (n < )
return n;
int[] res = new int[n + ];
res[] = ;
res[] = ;
for (int i = ; i <= n; i++) {
res[i] = res[i - ] + res[i - ];
}
return res[n];
}
/*
* 方法一:递归 时间复杂度高
*/
public int climbStairs_1(int n) {
if (n < )
return ;
if (n == )
return ;
if (n == )
return ;
return climbStairs_1(n - ) + climbStairs_1(n - );
}
斐波那契数列
class Solution {
public:
int climbStairs(int n) {
int f = ;
int g = ;
while(n--){
f += g;
g = f -g;
}
return f;
}
};
climbing-stairs-动态规划,爬楼梯的路径数的更多相关文章
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- 力扣—climbing stairs(爬楼梯) python实现
题目描述: 中文: 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 英文: You are cl ...
- 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 70 Climbing Stairs(爬楼梯)(动态规划)(*)
翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 You are climbing a stair case. It tak ...
- LeetCode746 Min Cost Climbing Stairs(爬上楼梯的最小损失)
题目 On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you p ...
- 动态规划-爬楼梯问题java实现
最近开始看算法导论,研究了一下动态规划,下面就开始直入主题开始记录近期看的第一个知识点动态规划.提起动态规划就不得不提几个动态规划的金典问题爬楼梯.国王金矿.背包问题.今天就仔细分析一下爬楼梯问题. ...
- 70. Climbing Stairs(动态规划)
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 【easy】746. Min Cost Climbing Stairs 动态规划
On a staircase, the i-th step has some non-negative cost cost[i]assigned (0 indexed). Once you pay t ...
- 746. Min Cost Climbing Stairs(动态规划)
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
随机推荐
- 【机器学习算法-python实现】矩阵去噪以及归一化
1.背景 项目须要,打算用python实现矩阵的去噪和归一化.用numpy这些数学库没有找到非常理想的函数.所以一怒之下自己用标准库写了一个去噪和归一化的算法,效率有点低,只是还能用,大家假设有 ...
- Android加密解密
随笔分类 - Android加密解密 Android数据加密之异或加密算法 摘要: 前言: 这几天被公司临时拉到去做Android IM即时通信协议实现,大致看了下他们定的协议,由于之前没有参与,据说 ...
- nginx 413 500报错
采用nginx作反向代理,出现了一个诡异的问题,小文件可以提交,大文件会报500内部错误.这个是什么原因导致的呢? 查wiki可知,上传文件大小相关的有三个配置 client_body_buffer_ ...
- Java命令学习系列(六)——jinfo
jinfo可以输出java进程.core文件或远程debug服务器的配置信息.这些配置信息包括JAVA系统参数及命令行参数,如果进程运行在64位虚拟机上,需要指明-J-d64参数,如:jinfo -J ...
- JAVA NIO non-blocking模式实现高并发服务器(转)
原文链接:JAVA NIO non-blocking模式实现高并发服务器 Java自1.4以后,加入了新IO特性,NIO. 号称new IO. NIO带来了non-blocking特性. 这篇文章主要 ...
- hdu4753 Fishhead’s Little Game 状态压缩,总和一定的博弈
此题和UVA 10891 Game of Sum 总和一定的博弈,区间dp是一个道理,就是预处理麻烦 这是南京网络赛的一题,一直没做,今天做了,虽然时间有点长,但是1ac,这几乎是南京现场赛的最后一道 ...
- 为 hexo NexT 添加 Gitment 评论插件
Gitment 是作者imsun实现的一款基于 GitHub Issues 的评论系统. 支持在前端直接引入, 不需要任何后端代码. 可以在页面进行登录, 查看, 评论, 点赞等操作. 同时有完整的 ...
- Tensorflow LSTM实现
Tensorflow[LSTM] 0.背景 通过对<tensorflow machine learning cookbook>第9章第3节"implementing_lstm ...
- hadoop无法启动
dataNode 无法启动是配置过程中最常见的问题,主要原因是多次format namenode 造成namenode 和datanode的clusterID不一致.建议查看datanode上面的lo ...
- React从0到1
本篇将一直更新下去,写的多了,可能会拆成小章节,记录完整的学习笔记 github https://github.com/ae6623/ReactL