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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. Algorithm: dynamic programming

    1. Longest Increasing Subsequence (LIS) problem unsorted array, calculate out the maximum length of ...

  4. [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, ...

  5. hdu 4972 A simple dynamic programming problem(高效)

    pid=4972" target="_blank" style="">题目链接:hdu 4972 A simple dynamic progra ...

  6. HDU-4972 A simple dynamic programming problem

    http://acm.hdu.edu.cn/showproblem.php?pid=4972 ++和+1还是有区别的,不可大意. A simple dynamic programming proble ...

  7. 以计算斐波那契数列为例说说动态规划算法(Dynamic Programming Algorithm Overlapping subproblems Optimal substructure Memoization Tabulation)

    动态规划(Dynamic Programming)是求解决策过程(decision process)最优化的数学方法.它的名字和动态没有关系,是Richard Bellman为了唬人而取的. 动态规划 ...

  8. [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 ...

  9. Dynamic Programming

    We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...

随机推荐

  1. HTML中的ul, ol,li , dl,dt, dd标签

    ul: unordered lists ol: ordered lists li: Lists ol 有序列表. <ol><li>……</li><li> ...

  2. 深度理解python中的元类

    本文转自:(英文版)https://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python   (翻译版)   http:// ...

  3. NOIP初赛前一日记

    2018年10月12日,早晨7:25于机房. 早晨的鄞中,晨风还有点清冷.看着电脑上翻遍的资料,心里实在是有一种说不出的感觉. 说出来也算是丢脸——作为浙江选手,我为了NOIP2018的初赛,停课了一 ...

  4. PHP 笔记——基础

    一.PHP 简介 1. PHP是什么 PHP:Hypertext Preprocessor,即超文本预处理器. PHP是一种跨平台.服务器端.可嵌入HTML文件的脚本语言. 嵌入了PHP代码的HTML ...

  5. luoguP3250 [HNOI2016]网络 树链剖分 + 堆

    机房某大佬告诉我,一条链在全局线段树中的区间最多有$log$段 因此同样的,代表不在这条链上的区间同样只有$log$段 对这$log$段区间进行维护即可 为了能够删除,在线段树的每个节点暴力维护一个堆 ...

  6. Ubuntu 12.04下spark1.0.0 集群搭建(原创)

    spark1.0.0新版本的于2014-05-30正式发布啦,新的spark版本带来了很多新的特性,提供了更好的API支持,spark1.0.0增加了Spark SQL组件,增强了标准库(ML.str ...

  7. 升级到php7和安装拓展(mac centos)

    Mac升级到php7 使用homebrew安装php7 brew update #更新源 brew search php #查找源中的php,发现有php7.1版本,安装最新的php7.1 brew ...

  8. bzoj 2286

    第一道"虚树"题目(好吧,我也不知道这是不是虚树,但和虚树的思想肯定是一样的,都是简化树结构) 这一类算法核心思想都是简化树结构,只取我们必须的节点和一些信息,然后在简化后的树结构 ...

  9. [转]Android学习笔记:TabHost 和 FragmentTabHost

    TabHost 命名空间: android.widget.TabHost 初始化函数(必须在addTab之前调用): setup(); 包含两个子元素: 1.Tab标签容器TabWidget(@and ...

  10. CentOS依赖包查找工具(https://centos.pkgs.org)

    https://centos.pkgs.org 通过这个地址,可以搜索出一些常用包放在哪些依赖或者仓库上.