70. Climbing Stairs

Prompt

You are climbing a staircase. It takes n steps to reach the top.

Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

Approach

Based on the problem statement, we can take n steps to reach to the top of the staircase but the condition is that you can only take 1 or 2 steps at a time. Our task is to figure out how many distinct ways in which we can climb up the staircase.

Code

Python | Time: O(n) | Space: O(n)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
John Kim

John Kim

75 Followers

iOS Developer | Full Stack Developer | Software Engineer | LinkedIn: john-kim-developer | GitHub: cloudiosx | Portfolio: cloudiosx.com