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. Python并发编-用Event,线程检测数据库连接的例子

    尝试3次连接数据库 import time import random from threading import Thread,Event def connect_db(e): count = 0 ...

  2. 【Leetcode】583. Delete Operation for Two Strings

    583. Delete Operation for Two Strings Given two words word1 and word2, find the minimum number of st ...

  3. openstack多region配置

    实验 A机器 10.64.8.171     RegionOne B机器 10.64.8.142     RegionTwo         Keytson(这个组件随便放在哪台都可以) openst ...

  4. React Native 系列(二)

    前言 本系列是基于React Native版本号0.44.3写的,最初学习React Native的时候,完全没有接触过React和JS,本文的目的是为了给那些JS和React小白提供一个快速入门,让 ...

  5. poj 1659 Frogs' Neighborhood( 青蛙的邻居)

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 9639   Accepted: 40 ...

  6. 【BZOJ 2179】 2179: FFT快速傅立叶 (FFT)

    2179: FFT快速傅立叶 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 3308  Solved: 1720 Description 给出两个n位 ...

  7. NOI1999 JZYZOJ1289 棋盘分割 dp 方差的数学结论

    http://172.20.6.3/Problem_Show.asp?id=1289 除了下标一坨一坨屎一样挺恶心其他都还挺容易的dp,这道题才发现scanf保留小数位是四舍五入的,惊了. f[k][ ...

  8. BZOJ 1395 [Baltic2005]Trip(最短路+DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1354 [题目大意] 给出一些车的班次,包括起点,终点,到达起点时间区间, 到达终点时间 ...

  9. Yii2 init 与 beforeAction 区别

    1.执行顺序 init > beforeAction 2.调用子函数时,两个函数都不会再次执行 3.返回值 init返回false继续执行,beforeAction停止执行 4.执行EXIT,全 ...

  10. HDU 5575 Discover Water Tank 并查集 树形DP

    题意: 有一个水槽,边界的两块板是无穷高的,中间有n-1块隔板(有高度),现有一些条件(i,y,k),表示从左到右数的第i列中,在高度为(y+0.5)的地方是否有水(有水:k = 1),问最多能同时满 ...