data-structures · beginner · ~15 min
Count ways to climb n stairs taking 1 or 2 steps.
Implement:
long long climb_stairs(int n);
Return the number of distinct ways to climb n stairs taking 1 or 2 steps at a time. climb(0)=1. 0 <= n <= 90.
n in [0,90].
Number of ways.
Same recurrence as Fibonacci, shifted.
#include <stddef.h>
/* Number of ways to climb n stairs taking 1 or 2 steps at a time. climb(0)=1. 0<=n<=90. */
long long climb_stairs(int n){ (void)n; return 0; }
Off-by-one in the base cases (climb(0) is 1, not 0).
climb(0)=1, climb(1)=1, climb(2)=2.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.