Skip to content

Commit f4050e3

Browse files
committed
[Leo] 12th Week solutions (2,3)
1 parent b5889e7 commit f4050e3

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

longest-common-subsequence/Leo.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
3+
dp = [0] * (len(text2) + 1)
4+
5+
for i in range(len(text1)):
6+
prev_row = dp[:]
7+
for j in range(len(text2)):
8+
if text1[i] == text2[j]:
9+
dp[j + 1] = prev_row[j] + 1
10+
else:
11+
dp[j + 1] = max(dp[j], prev_row[j + 1])
12+
13+
return dp[-1]
14+
15+
## TC: O(mn), SC: O(n)

unique-paths/Leo.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def uniquePaths(self, m: int, n: int) -> int:
3+
dp = [1] * n
4+
5+
for _ in range(1, m):
6+
for i in range(1, n):
7+
dp[i] += dp[i - 1]
8+
9+
return dp[-1]
10+
## TC: O(mn), SC: O(n)
11+
12+
## return math.comb(m + n - 2, m - 1)
13+
## TC: O(1) whatever constant, SC: O(1)

0 commit comments

Comments
 (0)