Skip to content

Commit 6367ed8

Browse files
committed
feat: add solutions to lc problem: No.1035
No.1035.Uncrossed Lines
1 parent a6ef630 commit 6367ed8

File tree

4 files changed

+157
-1
lines changed

4 files changed

+157
-1
lines changed

solution/1000-1099/1035.Uncrossed Lines/README.md

+63-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,21 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65-
最长公共子序列问题
65+
**方法一:动态规划**
66+
67+
最长公共子序列问题。
68+
69+
定义 $dp[i][j]$ 表示数组 `nums1` 的前 $i$ 个元素和数组 `nums2` 的前 $j$ 个元素的最长公共子序列的长度。则有:
70+
71+
$$
72+
dp[i][j]=
73+
\begin{cases}
74+
dp[i-1][j-1]+1, & nums1[i-1]=nums2[j-1] \\
75+
\max(dp[i-1][j], dp[i][j-1]), & nums1[i-1]\neq nums2[j-1]
76+
\end{cases}
77+
$$
78+
79+
时间复杂度 $O(m\times n)$,空间复杂度 $O(m\times n)$。其中 $m$, $n$ 分别为数组 `nums1``nums2` 的长度。
6680

6781
<!-- tabs:start -->
6882

@@ -130,6 +144,54 @@ public:
130144
};
131145
```
132146
147+
### **Go**
148+
149+
```go
150+
func maxUncrossedLines(nums1 []int, nums2 []int) int {
151+
m, n := len(nums1), len(nums2)
152+
dp := make([][]int, m+1)
153+
for i := range dp {
154+
dp[i] = make([]int, n+1)
155+
}
156+
for i := 1; i <= m; i++ {
157+
for j := 1; j <= n; j++ {
158+
if nums1[i-1] == nums2[j-1] {
159+
dp[i][j] = dp[i-1][j-1] + 1
160+
} else {
161+
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
162+
}
163+
}
164+
}
165+
return dp[m][n]
166+
}
167+
168+
func max(a, b int) int {
169+
if a > b {
170+
return a
171+
}
172+
return b
173+
}
174+
```
175+
176+
### **TypeScript**
177+
178+
```ts
179+
function maxUncrossedLines(nums1: number[], nums2: number[]): number {
180+
const m = nums1.length;
181+
const n = nums2.length;
182+
const dp = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0));
183+
for (let i = 1; i <= m; ++i) {
184+
for (let j = 1; j <= n; ++j) {
185+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
186+
if (nums1[i - 1] == nums2[j - 1]) {
187+
dp[i][j] = dp[i - 1][j - 1] + 1;
188+
}
189+
}
190+
}
191+
return dp[m][n];
192+
}
193+
```
194+
133195
### **...**
134196

135197
```

solution/1000-1099/1035.Uncrossed Lines/README_EN.md

+56
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ We cannot draw 3 uncrossed lines, because the line from nums1[1] = 4 to nums2[2]
5353

5454
Longest common sub-sequences
5555

56+
$$
57+
dp[i][j]=
58+
\begin{cases}
59+
dp[i-1][j-1]+1, & nums1[i-1]=nums2[j-1] \\
60+
\max(dp[i-1][j], dp[i][j-1]), & nums1[i-1]\neq nums2[j-1]
61+
\end{cases}
62+
$$
63+
5664
<!-- tabs:start -->
5765

5866
### **Python3**
@@ -115,6 +123,54 @@ public:
115123
};
116124
```
117125
126+
### **Go**
127+
128+
```go
129+
func maxUncrossedLines(nums1 []int, nums2 []int) int {
130+
m, n := len(nums1), len(nums2)
131+
dp := make([][]int, m+1)
132+
for i := range dp {
133+
dp[i] = make([]int, n+1)
134+
}
135+
for i := 1; i <= m; i++ {
136+
for j := 1; j <= n; j++ {
137+
if nums1[i-1] == nums2[j-1] {
138+
dp[i][j] = dp[i-1][j-1] + 1
139+
} else {
140+
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
141+
}
142+
}
143+
}
144+
return dp[m][n]
145+
}
146+
147+
func max(a, b int) int {
148+
if a > b {
149+
return a
150+
}
151+
return b
152+
}
153+
```
154+
155+
### **TypeScript**
156+
157+
```ts
158+
function maxUncrossedLines(nums1: number[], nums2: number[]): number {
159+
const m = nums1.length;
160+
const n = nums2.length;
161+
const dp = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0));
162+
for (let i = 1; i <= m; ++i) {
163+
for (let j = 1; j <= n; ++j) {
164+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
165+
if (nums1[i - 1] == nums2[j - 1]) {
166+
dp[i][j] = dp[i - 1][j - 1] + 1;
167+
}
168+
}
169+
}
170+
return dp[m][n];
171+
}
172+
```
173+
118174
### **...**
119175

120176
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func maxUncrossedLines(nums1 []int, nums2 []int) int {
2+
m, n := len(nums1), len(nums2)
3+
dp := make([][]int, m+1)
4+
for i := range dp {
5+
dp[i] = make([]int, n+1)
6+
}
7+
for i := 1; i <= m; i++ {
8+
for j := 1; j <= n; j++ {
9+
if nums1[i-1] == nums2[j-1] {
10+
dp[i][j] = dp[i-1][j-1] + 1
11+
} else {
12+
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
13+
}
14+
}
15+
}
16+
return dp[m][n]
17+
}
18+
19+
func max(a, b int) int {
20+
if a > b {
21+
return a
22+
}
23+
return b
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function maxUncrossedLines(nums1: number[], nums2: number[]): number {
2+
const m = nums1.length;
3+
const n = nums2.length;
4+
const dp = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0));
5+
for (let i = 1; i <= m; ++i) {
6+
for (let j = 1; j <= n; ++j) {
7+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
8+
if (nums1[i - 1] == nums2[j - 1]) {
9+
dp[i][j] = dp[i - 1][j - 1] + 1;
10+
}
11+
}
12+
}
13+
return dp[m][n];
14+
}

0 commit comments

Comments
 (0)