Skip to content

Commit 02e7b40

Browse files
committed
D. J.:
- Added Top-Down (Memoization) Solutions for the Leetcode Problems 70 and 509
1 parent cb573c9 commit 02e7b40

File tree

4 files changed

+124
-8
lines changed

4 files changed

+124
-8
lines changed

awesome_python_leetcode/_509_fibonacci_number.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
"""Base class for all LeetCode Problems."""
33

4-
def fib(self, n: int) -> int:
4+
def fibTD(self, n: int) -> int:
55
"""
66
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the
77
Fibonacci sequence, such that each number is the sum of the two preceding ones,
@@ -12,10 +12,44 @@ def fib(self, n: int) -> int:
1212
1313
Given n, calculate F(n).
1414
"""
15+
# Top-Down Dynamic Programming (Memoization)
16+
# Time Complexity: O(n)
17+
# Space Complexity: O(n)
18+
19+
dp = {}
20+
21+
def dfs(m: int) -> int:
22+
if m == 0:
23+
return 0
24+
elif m == 1:
25+
return 1
26+
elif m in dp:
27+
return dp[m]
28+
else:
29+
dp[m - 1] = dfs(m - 1)
30+
dp[m - 2] = dfs(m - 2)
31+
dp[m] = dp[m - 1] + dp[m - 2]
32+
return dp[m]
33+
34+
return dfs(n)
35+
36+
def fibBU(self, n: int) -> int:
37+
"""
38+
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the
39+
Fibonacci sequence, such that each number is the sum of the two preceding ones,
40+
starting from 0 and 1. That is,
41+
42+
F(0) = 0, F(1) = 1
43+
F(n) = F(n - 1) + F(n - 2), for n > 1.
44+
45+
Given n, calculate F(n).
46+
"""
47+
# Bottom-Up Dynamic Programming (Tabulation)
48+
# Time Complexity: O(n)
49+
# Space Complexity: O(n)
50+
1551
if n == 0:
1652
return 0
17-
elif n <= 1:
18-
return 1
1953

2054
dp = [0 for _ in range(n + 1)]
2155
dp[1] = 1

awesome_python_leetcode/_70_climbing_stairs.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,55 @@
11
class Solution:
22
"""Base class for all LeetCode Problems."""
33

4-
def climbStairs(self, n: int) -> int:
4+
def climbStairsTD(self, n: int) -> int:
55
"""
66
You are climbing a staircase. It takes n steps to reach the top.
77
88
Each time you can either climb 1 or 2 steps. In how many distinct ways can you
99
climb to the top?
1010
"""
11+
# Time Complexity: O(n)
12+
# Space Complexity: O(n)
13+
dp = {}
14+
15+
def dfs(m: int) -> int:
16+
if m == 0:
17+
return 1
18+
elif m == 1:
19+
return 1
20+
elif m in dp:
21+
return dp[m]
22+
else:
23+
dp[m - 1] = dfs(m - 1)
24+
dp[m - 2] = dfs(m - 2)
25+
dp[m] = dp[m - 1] + dp[m - 2]
26+
return dp[m]
27+
28+
return dfs(n)
29+
30+
def climbStairsBU(self, n: int) -> int:
31+
"""
32+
You are climbing a staircase. It takes n steps to reach the top.
33+
34+
Each time you can either climb 1 or 2 steps. In how many distinct ways can you
35+
climb to the top?
36+
"""
37+
# Time Complexity: O(n)
38+
# Space Complexity: O(n)
39+
dp = [1 for _ in range(n + 1)]
40+
for i in range(2, n + 1):
41+
dp[i] = dp[i - 1] + dp[i - 2]
42+
return dp[-1]
43+
44+
def climbStairsBUOptimized(self, n: int) -> int:
45+
"""
46+
You are climbing a staircase. It takes n steps to reach the top.
47+
48+
Each time you can either climb 1 or 2 steps. In how many distinct ways can you
49+
climb to the top?
50+
"""
51+
# Time Complexity: O(n)
52+
# Space Complexity: O(1)
1153
one, two = 1, 1
1254
for i in range(n - 1):
1355
tmp = one

tests/test_509_fibonacci_number.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,21 @@
1111
(4, 3),
1212
],
1313
)
14-
def test_func(n: int, expected: int):
14+
def test_funcTD(n: int, expected: int):
1515
"""Tests the solution of a LeetCode problem."""
16-
fn = Solution().fib(n)
16+
fn = Solution().fibTD(n)
17+
assert fn == expected
18+
19+
20+
@pytest.mark.parametrize(
21+
argnames=["n", "expected"],
22+
argvalues=[
23+
(2, 1),
24+
(3, 2),
25+
(4, 3),
26+
],
27+
)
28+
def test_funcBU(n: int, expected: int):
29+
"""Tests the solution of a LeetCode problem."""
30+
fn = Solution().fibBU(n)
1731
assert fn == expected

tests/test_70_climbing_stairs.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,33 @@
1010
(3, 3),
1111
],
1212
)
13-
def test_func(n: int, expected: int):
13+
def test_funcTD(n: int, expected: int):
1414
"""Tests the solution of a LeetCode problem."""
15-
combinations = Solution().climbStairs(n)
15+
combinations = Solution().climbStairsTD(n)
16+
assert combinations == expected
17+
18+
19+
@pytest.mark.parametrize(
20+
argnames=["n", "expected"],
21+
argvalues=[
22+
(2, 2),
23+
(3, 3),
24+
],
25+
)
26+
def test_funcBU(n: int, expected: int):
27+
"""Tests the solution of a LeetCode problem."""
28+
combinations = Solution().climbStairsBU(n)
29+
assert combinations == expected
30+
31+
32+
@pytest.mark.parametrize(
33+
argnames=["n", "expected"],
34+
argvalues=[
35+
(2, 2),
36+
(3, 3),
37+
],
38+
)
39+
def test_funcBUOptimized(n: int, expected: int):
40+
"""Tests the solution of a LeetCode problem."""
41+
combinations = Solution().climbStairsBUOptimized(n)
1642
assert combinations == expected

0 commit comments

Comments
 (0)