Longest Common Subsequence Between Sorted Arrays - Problem
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 among 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.
The result should be sorted in strictly increasing order.
Input & Output
Example 1 — Basic Case
$
Input:
arrays = [[1,2,3,4],[1,2,3],[1,2,4]]
›
Output:
[1,2]
💡 Note:
Elements 1 and 2 appear in all three arrays. Element 3 appears in first two arrays only, element 4 appears in first and third arrays only.
Example 2 — Single Array
$
Input:
arrays = [[1,2,3]]
›
Output:
[1,2,3]
💡 Note:
With only one array, all elements [1,2,3] form the common subsequence.
Example 3 — No Common Elements
$
Input:
arrays = [[1,2],[3,4]]
›
Output:
[]
💡 Note:
No elements appear in both arrays, so the longest common subsequence is empty.
Constraints
- 1 ≤ arrays.length ≤ 1000
- 1 ≤ arrays[i].length ≤ 1000
- 1 ≤ arrays[i][j] ≤ 105
- arrays[i] is sorted in strictly increasing order
Visualization
Tap to expand
Understanding the Visualization
1
Input
Multiple sorted arrays with some overlapping elements
2
Process
Find elements present in every single array
3
Output
Common elements in sorted order
Key Takeaway
🎯 Key Insight: Count frequency of each element - those appearing in exactly n arrays form the longest common subsequence
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code