Skip to content

Commit 28b0b6e

Browse files
authored
Create 1035_Uncrossed_Lines.md
1 parent 5f9dd46 commit 28b0b6e

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

1035_Uncrossed_Lines.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
We write the integers of A and B (in the order they are given) on two separate horizontal lines.
2+
3+
Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that:
4+
5+
A[i] == B[j];
6+
The line we draw does not intersect any other connecting (non-horizontal) line.
7+
Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line.
8+
9+
Return the maximum number of connecting lines we can draw in this way.
10+
11+
12+
13+
Example 1:
14+
15+
16+
Input: A = [1,4,2], B = [1,2,4]
17+
Output: 2
18+
Explanation: We can draw 2 uncrossed lines as in the diagram.
19+
We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2.
20+
Example 2:
21+
22+
Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2]
23+
Output: 3
24+
Example 3:
25+
26+
Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1]
27+
Output: 2
28+
29+
30+
Note:
31+
32+
1 <= A.length <= 500
33+
1 <= B.length <= 500
34+
1 <= A[i], B[i] <= 2000
35+
36+
37+
```python
38+
def maxUncrossedLines(self, A: List[int], B: List[int]) -> int:
39+
40+
size = len(A)
41+
col_size = len(B)
42+
43+
dp = [[0] * (col_size+1) for _ in range(size+1)]
44+
for i in range(1, size+1):
45+
for j in range(1, col_size+1):
46+
if A[i-1] == B[j-1]:
47+
dp[i][j] = dp[i-1][j-1] + 1
48+
49+
dp[i][j] = max(dp[i][j], dp[i-1][j], dp[i][j-1])
50+
51+
return dp[-1][-1]
52+
53+
```
54+
55+
```
56+
Runtime: 224 ms, faster than 60.05% of Python3 online submissions for Uncrossed Lines.
57+
Memory Usage: 14.8 MB, less than 49.04% of Python3 online submissions for Uncrossed Lines.
58+
```

0 commit comments

Comments
 (0)