[LeetCode] Longest Common Subsequence Between Sorted Arrays

1940. Longest Common Subsequence Between Sorted Arrays

Given an array of integer arrays arrays where each arrays[i] is sorted in strictly increasing order, return an integer array representing the longest common subsequence between all the arrays.

A subsequence is a sequence that can be derived from another sequence by deleting some elements (possibly none) without changing the order of the remaining elements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
vector<int> longestCommonSubsequence(vector<vector<int>>& arrays) {
unordered_map<int, int> freq;
for(auto& A : arrays) {
for(auto& a : A)
freq[a]++;
}
vector<int> res;
for(int i = 1; i <= 100; i++) {
if(freq[i] == arrays.size()) res.push_back(i);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/08/PS/LeetCode/longest-common-subsequence-between-sorted-arrays/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.