[Algorithm -- Dynamic Programming] Recursive Staircase Problem
For example there is a staricase
N = 3
| ---|
|---| |
|---| |
---| |
There is N = 3 staricase, for each step, you can either take {1 or 2} step at a time. So asking how many ways you can get on N = 3 step:
Answer: should be 3 ways: {1,1,1,}, {1,2}, {2,1}.
Now assue N=0, there is only 1 way, writing a function which takes number N and return the number of ways to get on Nth step.
Solution: The solution can involve recursion. We can use Dynamice programming, bottom up approach:
function num_ways_bottom_ip(n) {
let nums = []; if (n === 0 || n === 1) {
return 1;
}
nums[0] = nums[1] = 1;
for (let i = 2; i <= n; i++) {
nums[i] = nums[i - 1] + nums[i - 2];
} return nums[n];
} console.log(num_ways_bottom_ip(5)); //
This now takes O(N * |X|) time and O(N) space. X is the step allow to take , in our case, is 2.
Now if the requirements changes form only take {1, 2} steps, to you can take {1,3,5} each at a time; How you could solve the problem;
The idea is pretty similar to {1,2} steps.
nums(i) = nums(i-1) + nums(i-2):
Therefore for {1.3.5} is equals:
nums(1) = nums(i-1) + nums(i-3) + nums(i-5)
We just need to make sure i-3, i-5 should be greater than 0.
function num_ways_bottom_up_X(n, x) {
let nums = []; if (n === 0) {
return 1;
}
nums[0] = 1; for (let i = 1; i <= n; i++) {
let total = 0;
for (let j of x) {
if (i - j >= 0) {
total += nums[i - j];
}
}
nums[i] = total;
} return nums[n];
} console.log(num_ways_bottom_up_X(5, [1,3,5])); //
[Algorithm -- Dynamic Programming] Recursive Staircase Problem的更多相关文章
- hdu 1159, LCS, dynamic programming, recursive backtrack vs iterative backtrack vs incremental, C++ 分类: hdoj 2015-07-10 04:14 112人阅读 评论(0) 收藏
thanks prof. Abhiram Ranade for his vedio on Longest Common Subsequence 's back track search view in ...
- [Algorithm -- Dynamic programming] How Many Ways to Decode This Message?
For example we have 'a' -> 1 'b' -> 2 .. 'z' -> 26 By given "12", we can decode t ...
- Algorithm: dynamic programming
1. Longest Increasing Subsequence (LIS) problem unsorted array, calculate out the maximum length of ...
- [Algorithm] Dynamic programming: Find Sets Of Numbers That Add Up To 16
For a given array, we try to find set of pair which sums up as the given target number. For example, ...
- hdu 4972 A simple dynamic programming problem(高效)
pid=4972" target="_blank" style="">题目链接:hdu 4972 A simple dynamic progra ...
- HDU-4972 A simple dynamic programming problem
http://acm.hdu.edu.cn/showproblem.php?pid=4972 ++和+1还是有区别的,不可大意. A simple dynamic programming proble ...
- 以计算斐波那契数列为例说说动态规划算法(Dynamic Programming Algorithm Overlapping subproblems Optimal substructure Memoization Tabulation)
动态规划(Dynamic Programming)是求解决策过程(decision process)最优化的数学方法.它的名字和动态没有关系,是Richard Bellman为了唬人而取的. 动态规划 ...
- [Algorithms] Using Dynamic Programming to Solve longest common subsequence problem
Let's say we have two strings: str1 = 'ACDEB' str2 = 'AEBC' We need to find the longest common subse ...
- Dynamic Programming
We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...
随机推荐
- ActiveMQ 权限(一)
在 ActiveMQ 认证(一) 中,若用户名或密码不正确,不能连接到ActiveMQ.我们可以通过配置文件,确用户是否有消息的读取.写入和管理的权限. 在plugin配置节点下,配置以下信息: &l ...
- Kali Linux缺少ifconfig命令
Kali Linux缺少ifconfig命令 ifconfig是配置和查看网络的基础命令.在某些Kali Linux版本中,可能会缺少ifconfig命令.这个时候,用户需要手动安装该命令.该命令 ...
- Sublime Text的使用技巧
来到腾讯之后,基本上整个团队都在使用Sublime Text这款编辑神器.虽说自己以前在写python的时候略有接触过,但只是把它当做简单的文本编辑器.来到这边后,才逐渐的体会到这款神作的牛逼之处. ...
- NOIP2018提高组题解
D1T1:铺设道路 回忆NOIP2013D2T1 积木大赛,发现这两题唯一的区别就是一个是造山一个是填坑,而把填坑的操作反序就是造山,所以可以直接使用那道题的方法. 具体方法是,从左到右每次考虑新的一 ...
- [BZOJ 4071] 巴邻旁之桥
Link: BZOJ 4071传送门 Solution: 首先算出能提前算的贡献 $K=1$:肯定选中间的点,小学数学 $K=2$:对于每对$(x,y)$一定选离$(x+y)/2$近的桥 也就是说将$ ...
- dcoker常用命令
记录一下常用的命令 docker run -t -i xxxx /bin/bash 运行容器的交互会话shell docker start xxxx 启动容器 docker stop xxxx 停止 ...
- 【ArcGIS笔记】数据处理
1.ARCGIS在导入Excel坐标点的时候出现"没有注册类"的情况怎么办? 确保你本机上装有office,并且版本要能够识别XLSX格式.2007以上. 2.导入excel时re ...
- PHP -- 8个必备的PHP功能开发
原文出处:http://www.codeceo.com/8-php-functions.html 做过PHP开发的程序员应该清楚,PHP中有很多内置的功能,掌握了它们,可以帮助你在做PHP开发时更加得 ...
- Codeforces Round #299 (Div. 1) A. Tavas and Karafs 水题
Tavas and Karafs Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/536/prob ...
- 2015 UESTC 搜索专题A题 王之迷宫 三维bfs
A - 王之迷宫 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...