Skip to content

Commit 9799821

Browse files
authored
feat: add solutions to lc problem: No.1940 (#4034)
No.1940.Longest Common Subsequence Between Sorted Arrays
1 parent c6fa041 commit 9799821

File tree

9 files changed

+230
-239
lines changed

9 files changed

+230
-239
lines changed

Diff for: solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README.md

+81-84
Original file line numberDiff line numberDiff line change
@@ -67,42 +67,50 @@ tags:
6767

6868
<!-- solution:start -->
6969

70-
### 方法一
70+
### 方法一:计数
71+
72+
我们注意到,元素的范围是 $[1, 100]$,我们可以用一个长度为 $101$ 的数组 $\textit{cnt}$ 来记录每个元素出现的次数。
73+
74+
由于数组 $\textit{arrays}$ 中的每个数组都是严格递增排序的,因此,公共子序列的元素必然是单调递增,并且这些元素的出现次数都等于 $\textit{arrays}$ 的长度。
75+
76+
因此,我们可以遍历 $\textit{arrays}$ 中的每个数组,统计每个元素的出现次数,最后,从小到大遍历 $\textit{cnt}$ 的每个元素,如果出现次数等于 $\textit{arrays}$ 的长度,那么这个元素就是公共子序列的元素之一,我们将其加入答案数组中。
77+
78+
遍历结束后,返回答案数组即可。
79+
80+
时间复杂度 $O(M + N)$,空间复杂度 $O(M)$。其中 $M$ 为元素的范围,本题中 $M = 101$,而 $N$ 为数组所有元素的个数。
7181

7282
<!-- tabs:start -->
7383

7484
#### Python3
7585

7686
```python
7787
class Solution:
78-
def longestCommomSubsequence(self, arrays: List[List[int]]) -> List[int]:
79-
n = len(arrays)
80-
counter = defaultdict(int)
81-
for array in arrays:
82-
for e in array:
83-
counter[e] += 1
84-
return [e for e, count in counter.items() if count == n]
88+
def longestCommonSubsequence(self, arrays: List[List[int]]) -> List[int]:
89+
cnt = [0] * 101
90+
for row in arrays:
91+
for x in row:
92+
cnt[x] += 1
93+
return [x for x, v in enumerate(cnt) if v == len(arrays)]
8594
```
8695

8796
#### Java
8897

8998
```java
9099
class Solution {
91-
public List<Integer> longestCommomSubsequence(int[][] arrays) {
92-
Map<Integer, Integer> counter = new HashMap<>();
93-
for (int[] array : arrays) {
94-
for (int e : array) {
95-
counter.put(e, counter.getOrDefault(e, 0) + 1);
100+
public List<Integer> longestCommonSubsequence(int[][] arrays) {
101+
int[] cnt = new int[101];
102+
for (var row : arrays) {
103+
for (int x : row) {
104+
++cnt[x];
96105
}
97106
}
98-
int n = arrays.length;
99-
List<Integer> res = new ArrayList<>();
100-
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
101-
if (entry.getValue() == n) {
102-
res.add(entry.getKey());
107+
List<Integer> ans = new ArrayList<>();
108+
for (int i = 0; i < 101; ++i) {
109+
if (cnt[i] == arrays.length) {
110+
ans.add(i);
103111
}
104112
}
105-
return res;
113+
return ans;
106114
}
107115
}
108116
```
@@ -112,39 +120,60 @@ class Solution {
112120
```cpp
113121
class Solution {
114122
public:
115-
vector<int> longestCommomSubsequence(vector<vector<int>>& arrays) {
116-
unordered_map<int, int> counter;
117-
vector<int> res;
118-
int n = arrays.size();
119-
for (auto array : arrays) {
120-
for (auto e : array) {
121-
counter[e] += 1;
122-
if (counter[e] == n) {
123-
res.push_back(e);
124-
}
123+
vector<int> longestCommonSubsequence(vector<vector<int>>& arrays) {
124+
int cnt[101]{};
125+
for (const auto& row : arrays) {
126+
for (int x : row) {
127+
++cnt[x];
125128
}
126129
}
127-
return res;
130+
vector<int> ans;
131+
for (int i = 0; i < 101; ++i) {
132+
if (cnt[i] == arrays.size()) {
133+
ans.push_back(i);
134+
}
135+
}
136+
return ans;
128137
}
129138
};
130139
```
131140
132141
#### Go
133142
134143
```go
135-
func longestCommomSubsequence(arrays [][]int) []int {
136-
counter := make(map[int]int)
137-
n := len(arrays)
138-
var res []int
139-
for _, array := range arrays {
140-
for _, e := range array {
141-
counter[e]++
142-
if counter[e] == n {
143-
res = append(res, e)
144-
}
144+
func longestCommonSubsequence(arrays [][]int) (ans []int) {
145+
cnt := [101]int{}
146+
for _, row := range arrays {
147+
for _, x := range row {
148+
cnt[x]++
149+
}
150+
}
151+
for x, v := range cnt {
152+
if v == len(arrays) {
153+
ans = append(ans, x)
145154
}
146155
}
147-
return res
156+
return
157+
}
158+
```
159+
160+
#### TypeScript
161+
162+
```ts
163+
function longestCommonSubsequence(arrays: number[][]): number[] {
164+
const cnt: number[] = Array(101).fill(0);
165+
for (const row of arrays) {
166+
for (const x of row) {
167+
++cnt[x];
168+
}
169+
}
170+
const ans: number[] = [];
171+
for (let i = 0; i < 101; ++i) {
172+
if (cnt[i] === arrays.length) {
173+
ans.push(i);
174+
}
175+
}
176+
return ans;
148177
}
149178
```
150179

@@ -156,56 +185,24 @@ func longestCommomSubsequence(arrays [][]int) []int {
156185
* @return {number[]}
157186
*/
158187
var longestCommonSubsequence = function (arrays) {
159-
const m = new Map();
160-
const rs = [];
161-
const len = arrays.length;
162-
for (let i = 0; i < len; i++) {
163-
for (let j = 0; j < arrays[i].length; j++) {
164-
m.set(arrays[i][j], (m.get(arrays[i][j]) || 0) + 1);
165-
if (m.get(arrays[i][j]) === len) rs.push(arrays[i][j]);
188+
const cnt = Array(101).fill(0);
189+
for (const row of arrays) {
190+
for (const x of row) {
191+
++cnt[x];
166192
}
167193
}
168-
return rs;
194+
const ans = [];
195+
for (let i = 0; i < 101; ++i) {
196+
if (cnt[i] === arrays.length) {
197+
ans.push(i);
198+
}
199+
}
200+
return ans;
169201
};
170202
```
171203

172204
<!-- tabs:end -->
173205

174206
<!-- solution:end -->
175207

176-
<!-- solution:start -->
177-
178-
### 方法二
179-
180-
<!-- tabs:start -->
181-
182-
#### Python3
183-
184-
```python
185-
class Solution:
186-
def longestCommomSubsequence(self, arrays: List[List[int]]) -> List[int]:
187-
def common(l1, l2):
188-
i, j, n1, n2 = 0, 0, len(l1), len(l2)
189-
res = []
190-
while i < n1 and j < n2:
191-
if l1[i] == l2[j]:
192-
res.append(l1[i])
193-
i += 1
194-
j += 1
195-
elif l1[i] > l2[j]:
196-
j += 1
197-
else:
198-
i += 1
199-
return res
200-
201-
n = len(arrays)
202-
for i in range(1, n):
203-
arrays[i] = common(arrays[i - 1], arrays[i])
204-
return arrays[n - 1]
205-
```
206-
207-
<!-- tabs:end -->
208-
209-
<!-- solution:end -->
210-
211208
<!-- problem:end -->

0 commit comments

Comments
 (0)