Problem Description
Given a 2D integer array nums where each nums[i] is a non-empty array of distinct positive integers, return the list of integers that are present in each array of nums sorted in ascending order.
Key Insights
- We need to compute the intersection of multiple arrays.
- Each array contains distinct integers, so there are no duplicate elements within any individual array.
- Efficient solutions can use sets (or hash tables) to compute intersections.
- After finding the common elements, the answer must be sorted in ascending order.
- The total number of integers across all arrays is relatively small, making simple iterative approaches viable.
Space and Time Complexity
Time Complexity: O(N * L + K log K) where N is the number of arrays, L is the average length of each array, and sorting K common elements takes O(K log K).
Space Complexity: O(M) where M is the maximum number of distinct elements (M <= 1000).
Solution
We approach the problem using set intersection. First, initialize a set with the elements from the first array. Then, iterate over the remaining arrays, updating the set to keep only elements present in both the current set and the next array. Finally, convert the resulting set into a sorted list before returning it. This method is efficient due to the small input size and leverages built-in set operations for clarity and conciseness.