diff --git a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README.md b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README.md index 42fead4048c98..025ac51702456 100644 --- a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README.md +++ b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README.md @@ -67,7 +67,17 @@ tags: -### 方法一 +### 方法一:计数 + +我们注意到,元素的范围是 $[1, 100]$,我们可以用一个长度为 $101$ 的数组 $\textit{cnt}$ 来记录每个元素出现的次数。 + +由于数组 $\textit{arrays}$ 中的每个数组都是严格递增排序的,因此,公共子序列的元素必然是单调递增,并且这些元素的出现次数都等于 $\textit{arrays}$ 的长度。 + +因此,我们可以遍历 $\textit{arrays}$ 中的每个数组,统计每个元素的出现次数,最后,从小到大遍历 $\textit{cnt}$ 的每个元素,如果出现次数等于 $\textit{arrays}$ 的长度,那么这个元素就是公共子序列的元素之一,我们将其加入答案数组中。 + +遍历结束后,返回答案数组即可。 + +时间复杂度 $O(M + N)$,空间复杂度 $O(M)$。其中 $M$ 为元素的范围,本题中 $M = 101$,而 $N$ 为数组所有元素的个数。 @@ -75,34 +85,32 @@ tags: ```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 longestCommomSubsequence(int[][] arrays) { - Map counter = new HashMap<>(); - for (int[] array : arrays) { - for (int e : array) { - counter.put(e, counter.getOrDefault(e, 0) + 1); + public List longestCommonSubsequence(int[][] arrays) { + int[] cnt = new int[101]; + for (var row : arrays) { + for (int x : row) { + ++cnt[x]; } } - int n = arrays.length; - List res = new ArrayList<>(); - for (Map.Entry entry : counter.entrySet()) { - if (entry.getValue() == n) { - res.add(entry.getKey()); + List ans = new ArrayList<>(); + for (int i = 0; i < 101; ++i) { + if (cnt[i] == arrays.length) { + ans.add(i); } } - return res; + return ans; } } ``` @@ -112,19 +120,20 @@ class Solution { ```cpp class Solution { public: - vector longestCommomSubsequence(vector>& arrays) { - unordered_map counter; - vector 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 longestCommonSubsequence(vector>& arrays) { + int cnt[101]{}; + for (const auto& row : arrays) { + for (int x : row) { + ++cnt[x]; } } - return res; + vector ans; + for (int i = 0; i < 101; ++i) { + if (cnt[i] == arrays.size()) { + ans.push_back(i); + } + } + return ans; } }; ``` @@ -132,19 +141,39 @@ public: #### 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,16 +185,19 @@ 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; }; ``` @@ -173,39 +205,4 @@ var longestCommonSubsequence = function (arrays) { - - -### 方法二 - - - -#### 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] -``` - - - - - diff --git a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README_EN.md b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README_EN.md index 069328ca532de..d529b91e77617 100644 --- a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README_EN.md +++ b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README_EN.md @@ -67,7 +67,17 @@ tags: -### Solution 1 +### Solution 1: Counting + +We note that the range of elements is $[1, 100]$, so we can use an array $\textit{cnt}$ of length $101$ to record the number of occurrences of each element. + +Since each array in $\textit{arrays}$ is strictly increasing, the elements of the common subsequence must be monotonically increasing, and the number of occurrences of these elements must be equal to the length of $\textit{arrays}$. + +Therefore, we can traverse each array in $\textit{arrays}$ and count the number of occurrences of each element. Finally, traverse each element of $\textit{cnt}$ from smallest to largest. If the number of occurrences is equal to the length of $\textit{arrays}$, then this element is one of the elements of the common subsequence, and we add it to the answer array. + +After the traversal, return the answer array. + +The time complexity is $O(M + N)$, and the space complexity is $O(M)$. Here, $M$ is the range of elements, and in this problem, $M = 101$, and $N$ is the total number of elements in the arrays. @@ -75,34 +85,32 @@ tags: ```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 longestCommomSubsequence(int[][] arrays) { - Map counter = new HashMap<>(); - for (int[] array : arrays) { - for (int e : array) { - counter.put(e, counter.getOrDefault(e, 0) + 1); + public List longestCommonSubsequence(int[][] arrays) { + int[] cnt = new int[101]; + for (var row : arrays) { + for (int x : row) { + ++cnt[x]; } } - int n = arrays.length; - List res = new ArrayList<>(); - for (Map.Entry entry : counter.entrySet()) { - if (entry.getValue() == n) { - res.add(entry.getKey()); + List ans = new ArrayList<>(); + for (int i = 0; i < 101; ++i) { + if (cnt[i] == arrays.length) { + ans.add(i); } } - return res; + return ans; } } ``` @@ -112,19 +120,20 @@ class Solution { ```cpp class Solution { public: - vector longestCommomSubsequence(vector>& arrays) { - unordered_map counter; - vector 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 longestCommonSubsequence(vector>& arrays) { + int cnt[101]{}; + for (const auto& row : arrays) { + for (int x : row) { + ++cnt[x]; } } - return res; + vector ans; + for (int i = 0; i < 101; ++i) { + if (cnt[i] == arrays.size()) { + ans.push_back(i); + } + } + return ans; } }; ``` @@ -132,19 +141,39 @@ public: #### 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,16 +185,19 @@ 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; }; ``` @@ -173,39 +205,4 @@ var longestCommonSubsequence = function (arrays) { - - -### Solution 2 - - - -#### 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] -``` - - - - - diff --git a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.cpp b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.cpp index 341598a732e7e..4420e2cec1cfa 100644 --- a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.cpp +++ b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.cpp @@ -1,17 +1,18 @@ class Solution { public: - vector longestCommomSubsequence(vector>& arrays) { - unordered_map counter; - vector 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 longestCommonSubsequence(vector>& arrays) { + int cnt[101]{}; + for (const auto& row : arrays) { + for (int x : row) { + ++cnt[x]; } } - return res; + vector ans; + for (int i = 0; i < 101; ++i) { + if (cnt[i] == arrays.size()) { + ans.push_back(i); + } + } + return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.go b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.go index 3e49fae6e6726..19f3030cd36f1 100644 --- a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.go +++ b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.go @@ -1,14 +1,14 @@ -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]++ } } - return res -} \ No newline at end of file + for x, v := range cnt { + if v == len(arrays) { + ans = append(ans, x) + } + } + return +} diff --git a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.java b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.java index 82178fe000a3d..e143fd2df01ae 100644 --- a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.java +++ b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.java @@ -1,18 +1,17 @@ class Solution { - public List longestCommomSubsequence(int[][] arrays) { - Map counter = new HashMap<>(); - for (int[] array : arrays) { - for (int e : array) { - counter.put(e, counter.getOrDefault(e, 0) + 1); + public List longestCommonSubsequence(int[][] arrays) { + int[] cnt = new int[101]; + for (var row : arrays) { + for (int x : row) { + ++cnt[x]; } } - int n = arrays.length; - List res = new ArrayList<>(); - for (Map.Entry entry : counter.entrySet()) { - if (entry.getValue() == n) { - res.add(entry.getKey()); + List ans = new ArrayList<>(); + for (int i = 0; i < 101; ++i) { + if (cnt[i] == arrays.length) { + ans.add(i); } } - return res; + return ans; } -} \ No newline at end of file +} diff --git a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.js b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.js index 0db3b375d48d4..42be4ffd411fd 100644 --- a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.js +++ b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.js @@ -3,14 +3,17 @@ * @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; }; diff --git a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.py b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.py index 0ad7b9bc17b1b..dab83817b506b 100644 --- a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.py +++ b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.py @@ -1,8 +1,7 @@ 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)] diff --git a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.ts b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.ts new file mode 100644 index 0000000000000..be79b18f6b69c --- /dev/null +++ b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution.ts @@ -0,0 +1,15 @@ +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; +} diff --git a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution2.py b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution2.py deleted file mode 100644 index 944e7249458e2..0000000000000 --- a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/Solution2.py +++ /dev/null @@ -1,20 +0,0 @@ -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]