Skip to content

Commit 49412c3

Browse files
committed
feat: Climbing Stairs
1 parent f0a2a39 commit 49412c3

File tree

7 files changed

+201
-1
lines changed

7 files changed

+201
-1
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"problem_name": "climbing_stairs",
3+
"solution_class_name": "Solution",
4+
"problem_number": "70",
5+
"problem_title": "Climbing Stairs",
6+
"difficulty": "Easy",
7+
"topics": "Math, Dynamic Programming, Memoization",
8+
"tags": ["grind-75"],
9+
"readme_description": "You are climbing a staircase. It takes `n` steps to reach the top.\n\nEach time you can either climb `1` or `2` steps. In how many distinct ways can you climb to the top?",
10+
"readme_examples": [
11+
{
12+
"content": "```\nInput: n = 2\nOutput: 2\n```\n**Explanation:** There are two ways to climb to the top.\n1. 1 step + 1 step\n2. 2 steps"
13+
},
14+
{
15+
"content": "```\nInput: n = 3\nOutput: 3\n```\n**Explanation:** There are three ways to climb to the top.\n1. 1 step + 1 step + 1 step\n2. 1 step + 2 steps\n3. 2 steps + 1 step"
16+
}
17+
],
18+
"readme_constraints": "- 1 <= n <= 45",
19+
"readme_additional": "",
20+
"solution_imports": "",
21+
"solution_methods": [
22+
{ "name": "climb_stairs", "parameters": "n: int", "return_type": "int", "dummy_return": "1" }
23+
],
24+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution",
25+
"test_class_name": "ClimbingStairs",
26+
"test_helper_methods": [
27+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
28+
],
29+
"test_methods": [
30+
{
31+
"name": "test_climb_stairs",
32+
"parametrize": "n, expected",
33+
"parametrize_typed": "n: int, expected: int",
34+
"test_cases": "[(1, 1), (2, 2), (3, 3), (4, 5), (5, 8), (6, 13), (10, 89), (20, 10946), (45, 1836311903)]",
35+
"body": "result = self.solution.climb_stairs(n)\nassert result == expected"
36+
}
37+
],
38+
"playground_imports": "from solution import Solution",
39+
"playground_test_case": "# Example test case\nn = 3\nexpected = 3",
40+
"playground_execution": "result = Solution().climb_stairs(n)\nresult",
41+
"playground_assertion": "assert result == expected"
42+
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PYTHON_VERSION = 3.13
2-
PROBLEM ?= first_bad_version
2+
PROBLEM ?= climbing_stairs
33
FORCE ?= 0
44
COMMA := ,
55

leetcode/climbing_stairs/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Climbing Stairs
2+
3+
**Difficulty:** Easy
4+
**Topics:** Math, Dynamic Programming, Memoization
5+
**Tags:** grind-75
6+
7+
**LeetCode:** [Problem 70](https://leetcode.com/problems/climbing-stairs/description/)
8+
9+
## Problem Description
10+
11+
You are climbing a staircase. It takes `n` steps to reach the top.
12+
13+
Each time you can either climb `1` or `2` steps. In how many distinct ways can you climb to the top?
14+
15+
## Examples
16+
17+
### Example 1:
18+
19+
```
20+
Input: n = 2
21+
Output: 2
22+
```
23+
24+
**Explanation:** There are two ways to climb to the top.
25+
26+
1. 1 step + 1 step
27+
2. 2 steps
28+
29+
### Example 2:
30+
31+
```
32+
Input: n = 3
33+
Output: 3
34+
```
35+
36+
**Explanation:** There are three ways to climb to the top.
37+
38+
1. 1 step + 1 step + 1 step
39+
2. 1 step + 2 steps
40+
3. 2 steps + 1 step
41+
42+
## Constraints
43+
44+
- 1 <= n <= 45

leetcode/climbing_stairs/__init__.py

Whitespace-only changes.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "imports",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from solution import Solution"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 2,
16+
"id": "setup",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# Example test case\n",
21+
"n = 3\n",
22+
"expected = 3"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 3,
28+
"id": "execute",
29+
"metadata": {},
30+
"outputs": [
31+
{
32+
"data": {
33+
"text/plain": [
34+
"3"
35+
]
36+
},
37+
"execution_count": 3,
38+
"metadata": {},
39+
"output_type": "execute_result"
40+
}
41+
],
42+
"source": [
43+
"result = Solution().climb_stairs(n)\n",
44+
"result"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 4,
50+
"id": "test",
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"assert result == expected"
55+
]
56+
}
57+
],
58+
"metadata": {
59+
"kernelspec": {
60+
"display_name": "leetcode-py-py3.13",
61+
"language": "python",
62+
"name": "python3"
63+
},
64+
"language_info": {
65+
"codemirror_mode": {
66+
"name": "ipython",
67+
"version": 3
68+
},
69+
"file_extension": ".py",
70+
"mimetype": "text/x-python",
71+
"name": "python",
72+
"nbconvert_exporter": "python",
73+
"pygments_lexer": "ipython3",
74+
"version": "3.13.7"
75+
}
76+
},
77+
"nbformat": 4,
78+
"nbformat_minor": 5
79+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
# Time: O(n)
3+
# Space: O(1)
4+
5+
# This follows Fibonacci pattern
6+
# Standard Fib: F(0)=0, F(1)=1, F(2)=1, F(3)=2, F(4)=3, F(5)=5...
7+
def climb_stairs(self, n: int) -> int:
8+
if n <= 2:
9+
return n
10+
11+
prev2, prev1 = 1, 2
12+
for _ in range(3, n + 1):
13+
current = prev1 + prev2
14+
prev2, prev1 = prev1, current
15+
16+
return prev1

leetcode/climbing_stairs/tests.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pytest
2+
3+
from leetcode_py.test_utils import logged_test
4+
5+
from .solution import Solution
6+
7+
8+
class TestClimbingStairs:
9+
def setup_method(self):
10+
self.solution = Solution()
11+
12+
@pytest.mark.parametrize(
13+
"n, expected",
14+
[(1, 1), (2, 2), (3, 3), (4, 5), (5, 8), (6, 13), (10, 89), (20, 10946), (45, 1836311903)],
15+
)
16+
@logged_test
17+
def test_climb_stairs(self, n: int, expected: int):
18+
result = self.solution.climb_stairs(n)
19+
assert result == expected

0 commit comments

Comments
 (0)