data-structures · intermediate · ~15 min
Count climbs with variable step sizes (memoized).
Implement:
long long count_staircase_ways(int n, int max_step);
Count the ways to climb n stairs taking between 1 and max_step steps at a time, using memoized recursion. count(0)=1.
n >= 0, max_step >= 1.
Number of ways.
Memoize to avoid exponential blowup.
#include <stddef.h>
/* Number of ways to climb n stairs taking between 1 and max_step steps at a time (memoized recursion). count(0)=1. */
long long count_staircase_ways(int n,int max_step){ (void)n;(void)max_step; return 0; }
Plain recursion without memoization is exponential and times out.
0 stairs has 1 way (do nothing).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.