Skip to content
/ leetcode Public
  • Sponsor doocs/leetcode

  • Notifications You must be signed in to change notification settings
  • Fork 9k

feat: add solutions to lc problem: No.1940 #4034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -67,42 +67,50 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:计数

我们注意到,元素的范围是 $[1, 100]$,我们可以用一个长度为 $101$ 的数组 $\textit{cnt}$ 来记录每个元素出现的次数。

由于数组 $\textit{arrays}$ 中的每个数组都是严格递增排序的,因此,公共子序列的元素必然是单调递增,并且这些元素的出现次数都等于 $\textit{arrays}$ 的长度。

因此,我们可以遍历 $\textit{arrays}$ 中的每个数组,统计每个元素的出现次数,最后,从小到大遍历 $\textit{cnt}$ 的每个元素,如果出现次数等于 $\textit{arrays}$ 的长度,那么这个元素就是公共子序列的元素之一,我们将其加入答案数组中。

遍历结束后,返回答案数组即可。

时间复杂度 $O(M + N)$,空间复杂度 $O(M)$。其中 $M$ 为元素的范围,本题中 $M = 101$,而 $N$ 为数组所有元素的个数。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def longestCommomSubsequence(self, arrays: List[List[int]]) -> List[int]:
n = len(arrays)
counter = defaultdict(int)
for array in arrays:
for e in array:
counter[e] += 1
return [e for e, count in counter.items() if count == n]
def longestCommonSubsequence(self, arrays: List[List[int]]) -> List[int]:
cnt = [0] * 101
for row in arrays:
for x in row:
cnt[x] += 1
return [x for x, v in enumerate(cnt) if v == len(arrays)]
```

#### Java

```java
class Solution {
public List<Integer> longestCommomSubsequence(int[][] arrays) {
Map<Integer, Integer> counter = new HashMap<>();
for (int[] array : arrays) {
for (int e : array) {
counter.put(e, counter.getOrDefault(e, 0) + 1);
public List<Integer> longestCommonSubsequence(int[][] arrays) {
int[] cnt = new int[101];
for (var row : arrays) {
for (int x : row) {
++cnt[x];
}
}
int n = arrays.length;
List<Integer> res = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
if (entry.getValue() == n) {
res.add(entry.getKey());
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < 101; ++i) {
if (cnt[i] == arrays.length) {
ans.add(i);
}
}
return res;
return ans;
}
}
```
@@ -112,39 +120,60 @@ class Solution {
```cpp
class Solution {
public:
vector<int> longestCommomSubsequence(vector<vector<int>>& arrays) {
unordered_map<int, int> counter;
vector<int> res;
int n = arrays.size();
for (auto array : arrays) {
for (auto e : array) {
counter[e] += 1;
if (counter[e] == n) {
res.push_back(e);
}
vector<int> longestCommonSubsequence(vector<vector<int>>& arrays) {
int cnt[101]{};
for (const auto& row : arrays) {
for (int x : row) {
++cnt[x];
}
}
return res;
vector<int> ans;
for (int i = 0; i < 101; ++i) {
if (cnt[i] == arrays.size()) {
ans.push_back(i);
}
}
return ans;
}
};
```
#### Go
```go
func longestCommomSubsequence(arrays [][]int) []int {
counter := make(map[int]int)
n := len(arrays)
var res []int
for _, array := range arrays {
for _, e := range array {
counter[e]++
if counter[e] == n {
res = append(res, e)
}
func longestCommonSubsequence(arrays [][]int) (ans []int) {
cnt := [101]int{}
for _, row := range arrays {
for _, x := range row {
cnt[x]++
}
}
for x, v := range cnt {
if v == len(arrays) {
ans = append(ans, x)
}
}
return res
return
}
```

#### TypeScript

```ts
function longestCommonSubsequence(arrays: number[][]): number[] {
const cnt: number[] = Array(101).fill(0);
for (const row of arrays) {
for (const x of row) {
++cnt[x];
}
}
const ans: number[] = [];
for (let i = 0; i < 101; ++i) {
if (cnt[i] === arrays.length) {
ans.push(i);
}
}
return ans;
}
```

@@ -156,56 +185,24 @@ func longestCommomSubsequence(arrays [][]int) []int {
* @return {number[]}
*/
var longestCommonSubsequence = function (arrays) {
const m = new Map();
const rs = [];
const len = arrays.length;
for (let i = 0; i < len; i++) {
for (let j = 0; j < arrays[i].length; j++) {
m.set(arrays[i][j], (m.get(arrays[i][j]) || 0) + 1);
if (m.get(arrays[i][j]) === len) rs.push(arrays[i][j]);
const cnt = Array(101).fill(0);
for (const row of arrays) {
for (const x of row) {
++cnt[x];
}
}
return rs;
const ans = [];
for (let i = 0; i < 101; ++i) {
if (cnt[i] === arrays.length) {
ans.push(i);
}
}
return ans;
};
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Python3

```python
class Solution:
def longestCommomSubsequence(self, arrays: List[List[int]]) -> List[int]:
def common(l1, l2):
i, j, n1, n2 = 0, 0, len(l1), len(l2)
res = []
while i < n1 and j < n2:
if l1[i] == l2[j]:
res.append(l1[i])
i += 1
j += 1
elif l1[i] > l2[j]:
j += 1
else:
i += 1
return res

n = len(arrays)
for i in range(1, n):
arrays[i] = common(arrays[i - 1], arrays[i])
return arrays[n - 1]
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading