Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yuanyu90221/leetcode_climbing_stairs
https://github.com/yuanyu90221/leetcode_climbing_stairs
Last synced: 16 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/yuanyu90221/leetcode_climbing_stairs
- Owner: yuanyu90221
- Created: 2021-07-16T15:56:28.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-07-16T15:57:23.000Z (over 3 years ago)
- Last Synced: 2024-06-20T22:14:21.701Z (7 months ago)
- Language: Go
- Size: 1.95 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# leetcode_climbing_stairs
This repository is for solve [climbing-stairs](https://leetcode.com/problems/climbing-stairs/)
## problem description
You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
## Examples
### Example1
```
Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
```
### Example2
```
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
```
## Observation
```
Notice thatThere exists a relation
climb n stairs methods = first Step is One Step + first Step is Two Steps
= climb n - 1 stairs method + climb n - 2 stairs method
for n >= 3
```
## implemtationsThere are two implemention for solve this problem
Solution 1:
Create an array to store previous to accumlate which take Space Complexity O(n)
and iterate n step to compute n times
Thus, Time Complexity is O(n)
Solution 2:
Use iteration to accumlate previous two steps result
iterate n steps to compute
Time Complexity is O(n)