Skip to content

Commit 081c9dd

Browse files
committed
- Climbing Stairs #230
1 parent 4ba78fc commit 081c9dd

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

climbing-stairs/ayosecu.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution:
2+
"""
3+
- Time Complexity: O(n)
4+
- Space Complexity: O(n)
5+
"""
6+
def climbStairs(self, n: int) -> int:
7+
"""
8+
- DP Formation
9+
- dp[0] = 1
10+
- dp[1] = 1
11+
- dp[2] = dp[1] + dp[0] = 2
12+
- dp[3] = dp[2] + dp[1] = 3
13+
- dp[i] = dp[i - 1] + dp[i - 2]
14+
"""
15+
if n <= 1:
16+
return 1
17+
18+
dp = [1] * (n + 1)
19+
20+
for i in range(2, n + 1):
21+
dp[i] = dp[i - 1] + dp[i - 2]
22+
23+
return dp[-1]
24+
25+
26+
tc = [
27+
(1, 1),
28+
(2, 2),
29+
(3, 3),
30+
(4, 5)
31+
]
32+
33+
for i, (n, e) in enumerate(tc, 1):
34+
sol = Solution()
35+
r = sol.climbStairs(n)
36+
print(f"TC {i} is Passed!" if r == e else f"TC {i} is Failed! - Expected: {e}, Result: {r}")

0 commit comments

Comments
 (0)