Skip to content

Commit 52cc3d5

Browse files
committed
solve 4
1 parent 72ea223 commit 52cc3d5

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

longest-common-subsequence/pmjuu.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
시간 복잡도: O(m * n)
3+
공간 복잡도: O(n)
4+
'''
5+
6+
class Solution:
7+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
8+
m, n = len(text1), len(text2)
9+
prev = [0] * (n + 1)
10+
11+
for i in range(1, m + 1):
12+
curr = [0] * (n + 1)
13+
for j in range(1, n + 1):
14+
if text1[i - 1] == text2[j - 1]:
15+
curr[j] = prev[j - 1] + 1
16+
else:
17+
curr[j] = max(prev[j], curr[j - 1])
18+
prev = curr # 현재 행을 이전 행으로 업데이트
19+
20+
return prev[n]

0 commit comments

Comments
 (0)