[AlgoExpert] Staircase Traversal

Staircase Traversal

  • Time : O(n)
  • Space : O(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
int staircaseTraversal(int h, int s) {
vector<int> dp(h + 1, 0);
dp[0] = 1;
for(int i = 1, sum = 0; i <= h; i++) {
int st = i - s - 1;
int ed = i - 1;
if(st >= 0)
sum -= dp[st];
sum += dp[ed];
dp[i] = sum;
}
return dp[h];
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/14/PS/AlgoExpert/staircase-traversal/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.