Skip to content

Commit 5baa9e5

Browse files
authored
Create uncrossed-lines.py
1 parent 5307a15 commit 5baa9e5

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Python/uncrossed-lines.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(m * n)
2+
# Space: O(min(m, n))
3+
4+
class Solution(object):
5+
def maxUncrossedLines(self, A, B):
6+
"""
7+
:type A: List[int]
8+
:type B: List[int]
9+
:rtype: int
10+
"""
11+
if len(A) < len(B):
12+
return self.maxUncrossedLines(B, A)
13+
14+
dp = [[0 for _ in xrange(len(B)+1)] for _ in xrange(2)]
15+
for i in xrange(len(A)):
16+
for j in xrange(len(B)):
17+
dp[(i+1)%2][j+1] = max(dp[i%2][j] + int(A[i] == B[j]),
18+
dp[i%2][j+1],
19+
dp[(i+1)%2][j])
20+
return dp[len(A)%2][len(B)]

0 commit comments

Comments
 (0)