Problems/Dynamic Programming/Climbing Stairs
Dynamic Programming
easy

Climbing Stairs

This problem explores dynamic programming through counting possible paths. Mastering this pattern is crucial for demonstrating problem-solving and optimization skills in coding interviews.

dynamic programmingrecursionoptimizationmemoizationtabulation

Problem Statement

Imagine you're designing a video game where a character needs to cross a series of platforms. The character can jump one or three platforms at a time. Given a total number of platforms, determine the number of distinct ways the character can reach the final platform.

Example 1
Input: n = 5
Output: 2
There are two ways to reach the 5th platform: (1, 1, 1, 1, 1) and (1, 1, 3) and (1,3,1) and (3,1,1) and (3,2) and (2,3) so only 2 ways using only 1 or 3 platforms. (1,1,1,1,1) -> (1,1,3) -> (3,1,1) -> (1,3,1)
Example 2
Input: n = 7
Output: 3
There are three ways to reach the 7th platform: (1, 1, 1, 1, 1, 1, 1) -> (1,1,1,1,3) -> (1,3,3) -> (3,1,3) -> (3,3,1) -> (1,1,1,3,1) -> (1,1,3,1,1) -> (1,3,1,1,1) -> (3,1,1,1,1)
Constraints
  • -1 <= n <= 30
  • -n is an integer

Brute Force Approach

The brute-force approach would involve exploring every single possible combination of 1-platform and 3-platform jumps. Imagine physically trying every possible jump sequence until you reach the end - a very tedious process. This leads to exponential time complexity because each platform gives us multiple branching possibilities.

TimeO(2^n)
SpaceO(n)

Ready to practice?

Work through this problem with AI coaching and get real-time feedback

Practice This Problem