Skip to content

Commit 18e550f

Browse files
committed
+ problem 1035
1 parent e9b6586 commit 18e550f

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# 1035. Uncrossed Lines
2+
You are given two integer arrays `nums1` and `nums2`. We write the integers of `nums1` and `nums2` (in the order they are given) on two separate horizontal lines.
3+
4+
We may draw connecting lines: a straight line connecting two numbers `nums1[i]` and `nums2[j]` such that:
5+
6+
* `nums1[i] == nums2[j]`, and
7+
* the line we draw does not intersect any other connecting (non-horizontal) line.
8+
9+
Note that a connecting line cannot intersect even at the endpoints (i.e., each number can only belong to one connecting line).
10+
11+
Return *the maximum number of connecting lines we can draw in this way*.
12+
13+
#### Example 1:
14+
![](https://assets.leetcode.com/uploads/2019/04/26/142.png)
15+
<pre>
16+
<strong>Input:</strong> nums1 = [1,4,2], nums2 = [1,2,4]
17+
<strong>Output:</strong> 2
18+
<strong>Explanation:</strong> We can draw 2 uncrossed lines as in the diagram.
19+
We cannot draw 3 uncrossed lines, because the line from nums1[1] = 4 to nums2[2] = 4 will intersect the line from nums1[2]=2 to nums2[1]=2.
20+
</pre>
21+
22+
#### Example 2:
23+
<pre>
24+
<strong>Input:</strong> nums1 = [2,5,1,2,5], nums2 = [10,5,2,1,5,2]
25+
<strong>Output:</strong> 3
26+
</pre>
27+
28+
#### Example 3:
29+
<pre>
30+
<strong>Input:</strong> nums1 = [1,3,7,1,7,5], nums2 = [1,9,2,5,1]
31+
<strong>Output:</strong> 2
32+
</pre>
33+
34+
#### Constraints:
35+
* `1 <= nums1.length, nums2.length <= 500`
36+
* `1 <= nums1[i], nums2[j] <= 2000`
37+
38+
## Solutions (Rust)
39+
40+
### 1. Solution
41+
```Rust
42+
impl Solution {
43+
pub fn max_uncrossed_lines(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
44+
let m = nums1.len();
45+
let n = nums2.len();
46+
let mut dp = vec![vec![0; n]; m];
47+
48+
for i in 0..m {
49+
for j in 0..n {
50+
if nums1[i] == nums2[j] {
51+
if i > 0 && j > 0 {
52+
dp[i][j] = dp[i - 1][j - 1];
53+
}
54+
dp[i][j] += 1;
55+
}
56+
if i > 0 {
57+
dp[i][j] = dp[i][j].max(dp[i - 1][j]);
58+
}
59+
if j > 0 {
60+
dp[i][j] = dp[i][j].max(dp[i][j - 1]);
61+
}
62+
}
63+
}
64+
65+
dp[m - 1][n - 1]
66+
}
67+
}
68+
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# 1035. 不相交的线
2+
在两条独立的水平线上按给定的顺序写下 `nums1``nums2` 中的整数。
3+
4+
现在,可以绘制一些连接两个数字 `nums1[i]``nums2[j]` 的直线,这些直线需要同时满足满足:
5+
6+
* `nums1[i] == nums2[j]`
7+
* 且绘制的直线不与任何其他连线(非水平线)相交。
8+
9+
请注意,连线即使在端点也不能相交:每个数字只能属于一条连线。
10+
11+
以这种方法绘制线条,并返回可以绘制的最大连线数。
12+
13+
#### 示例 1:
14+
![](https://assets.leetcode.com/uploads/2019/04/26/142.png)
15+
<pre>
16+
<strong>输入:</strong> nums1 = [1,4,2], nums2 = [1,2,4]
17+
<strong>输出:</strong> 2
18+
<strong>解释:</strong> 可以画出两条不交叉的线,如上图所示。
19+
但无法画出第三条不相交的直线,因为从 nums1[1]=4 到 nums2[2]=4 的直线将与从 nums1[2]=2 到 nums2[1]=2 的直线相交。
20+
</pre>
21+
22+
#### 示例 2:
23+
<pre>
24+
<strong>输入:</strong> nums1 = [2,5,1,2,5], nums2 = [10,5,2,1,5,2]
25+
<strong>输出:</strong> 3
26+
</pre>
27+
28+
#### 示例 3:
29+
<pre>
30+
<strong>输入:</strong> nums1 = [1,3,7,1,7,5], nums2 = [1,9,2,5,1]
31+
<strong>输出:</strong> 2
32+
</pre>
33+
34+
#### 提示:
35+
* `1 <= nums1.length, nums2.length <= 500`
36+
* `1 <= nums1[i], nums2[j] <= 2000`
37+
38+
## 题解 (Rust)
39+
40+
### 1. 题解
41+
```Rust
42+
impl Solution {
43+
pub fn max_uncrossed_lines(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
44+
let m = nums1.len();
45+
let n = nums2.len();
46+
let mut dp = vec![vec![0; n]; m];
47+
48+
for i in 0..m {
49+
for j in 0..n {
50+
if nums1[i] == nums2[j] {
51+
if i > 0 && j > 0 {
52+
dp[i][j] = dp[i - 1][j - 1];
53+
}
54+
dp[i][j] += 1;
55+
}
56+
if i > 0 {
57+
dp[i][j] = dp[i][j].max(dp[i - 1][j]);
58+
}
59+
if j > 0 {
60+
dp[i][j] = dp[i][j].max(dp[i][j - 1]);
61+
}
62+
}
63+
}
64+
65+
dp[m - 1][n - 1]
66+
}
67+
}
68+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
impl Solution {
2+
pub fn max_uncrossed_lines(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
3+
let m = nums1.len();
4+
let n = nums2.len();
5+
let mut dp = vec![vec![0; n]; m];
6+
7+
for i in 0..m {
8+
for j in 0..n {
9+
if nums1[i] == nums2[j] {
10+
if i > 0 && j > 0 {
11+
dp[i][j] = dp[i - 1][j - 1];
12+
}
13+
dp[i][j] += 1;
14+
}
15+
if i > 0 {
16+
dp[i][j] = dp[i][j].max(dp[i - 1][j]);
17+
}
18+
if j > 0 {
19+
dp[i][j] = dp[i][j].max(dp[i][j - 1]);
20+
}
21+
}
22+
}
23+
24+
dp[m - 1][n - 1]
25+
}
26+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@
567567
[1030][1030l]|[Matrix Cells in Distance Order][1030] |![rs]
568568
[1031][1031l]|[Maximum Sum of Two Non-Overlapping Subarrays][1031] |![rs]
569569
[1033][1033l]|[Moving Stones Until Consecutive][1033] |![rb]&nbsp;&nbsp;![rs]
570+
[1035][1035l]|[Uncrossed Lines][1035] |![rs]
570571
[1037][1037l]|[Valid Boomerang][1037] |![rs]
571572
[1038][1038l]|[Binary Search Tree to Greater Sum Tree][1038] |![py]
572573
[1040][1040l]|[Moving Stones Until Consecutive II][1040] |![rs]
@@ -1806,6 +1807,7 @@
18061807
[1030]:Problemset/1030-Matrix%20Cells%20in%20Distance%20Order/README.md#1030-matrix-cells-in-distance-order
18071808
[1031]:Problemset/1031-Maximum%20Sum%20of%20Two%20Non-Overlapping%20Subarrays/README.md#1031-maximum-sum-of-two-non-overlapping-subarrays
18081809
[1033]:Problemset/1033-Moving%20Stones%20Until%20Consecutive/README.md#1033-moving-stones-until-consecutive
1810+
[1035]:Problemset/1035-Uncrossed%20Lines/README.md#1035-uncrossed-lines
18091811
[1037]:Problemset/1037-Valid%20Boomerang/README.md#1037-valid-boomerang
18101812
[1038]:Problemset/1038-Binary%20Search%20Tree%20to%20Greater%20Sum%20Tree/README.md#1038-binary-search-tree-to-greater-sum-tree
18111813
[1040]:Problemset/1040-Moving%20Stones%20Until%20Consecutive%20II/README.md#1040-moving-stones-until-consecutive-ii
@@ -3048,6 +3050,7 @@
30483050
[1030l]:https://leetcode.com/problems/matrix-cells-in-distance-order/
30493051
[1031l]:https://leetcode.com/problems/maximum-sum-of-two-non-overlapping-subarrays/
30503052
[1033l]:https://leetcode.com/problems/moving-stones-until-consecutive/
3053+
[1035l]:https://leetcode.com/problems/uncrossed-lines/
30513054
[1037l]:https://leetcode.com/problems/valid-boomerang/
30523055
[1038l]:https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/
30533056
[1040l]:https://leetcode.com/problems/moving-stones-until-consecutive-ii/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@
567567
[1030][1030l]|[距离顺序排列矩阵单元格][1030] |![rs]
568568
[1031][1031l]|[两个非重叠子数组的最大和][1031] |![rs]
569569
[1033][1033l]|[移动石子直到连续][1033] |![rb]&nbsp;&nbsp;![rs]
570+
[1035][1035l]|[不相交的线][1035] |![rs]
570571
[1037][1037l]|[有效的回旋镖][1037] |![rs]
571572
[1038][1038l]|[从二叉搜索树到更大和树][1038] |![py]
572573
[1040][1040l]|[移动石子直到连续 II][1040] |![rs]
@@ -1806,6 +1807,7 @@
18061807
[1030]:Problemset/1030-Matrix%20Cells%20in%20Distance%20Order/README_CN.md#1030-距离顺序排列矩阵单元格
18071808
[1031]:Problemset/1031-Maximum%20Sum%20of%20Two%20Non-Overlapping%20Subarrays/README_CN.md#1031-两个非重叠子数组的最大和
18081809
[1033]:Problemset/1033-Moving%20Stones%20Until%20Consecutive/README_CN.md#1033-移动石子直到连续
1810+
[1035]:Problemset/1035-Uncrossed%20Lines/README_CN.md#1035-不相交的线
18091811
[1037]:Problemset/1037-Valid%20Boomerang/README_CN.md#1037-有效的回旋镖
18101812
[1038]:Problemset/1038-Binary%20Search%20Tree%20to%20Greater%20Sum%20Tree/README_CN.md#1038-从二叉搜索树到更大和树
18111813
[1040]:Problemset/1040-Moving%20Stones%20Until%20Consecutive%20II/README_CN.md#1040-移动石子直到连续-ii
@@ -3048,6 +3050,7 @@
30483050
[1030l]:https://leetcode.cn/problems/matrix-cells-in-distance-order/
30493051
[1031l]:https://leetcode.cn/problems/maximum-sum-of-two-non-overlapping-subarrays/
30503052
[1033l]:https://leetcode.cn/problems/moving-stones-until-consecutive/
3053+
[1035l]:https://leetcode.cn/problems/uncrossed-lines/
30513054
[1037l]:https://leetcode.cn/problems/valid-boomerang/
30523055
[1038l]:https://leetcode.cn/problems/binary-search-tree-to-greater-sum-tree/
30533056
[1040l]:https://leetcode.cn/problems/moving-stones-until-consecutive-ii/

0 commit comments

Comments
 (0)