Intersection of Multiple Arrays — LeetCode 2248 Python Solution
EasyArrayHash TableCountingSorting
- Problem
- #2248
- Pattern
- Sorting
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Given a 2D integer array nums where 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.
Example
- Input
- nums = [[3,1,2,4,5],[1,2,3,4],[3,4,5,6]]
- Output
- [3,4]
- Explanation
- The only integers present in each of nums[0] = [3,1,2,4,5], nums[1] = [1,2,3,4], and nums[2] = [3,4,5,6] are 3 and 4, so we return [3,4].
Python solution
Python
class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
cnt = [0] * 1001
for arr in nums:
for x in arr:
cnt[x] += 1
return [x for x, v in enumerate(cnt) if v == len(nums)]Complexity
| Measure | Complexity |
|---|---|
| Time | O(N) |
| Space | O(1000) auxiliary |
Pattern: Sorting
Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial. LeetCode 2248. Intersection of Multiple Arrays is filed here because LeetCode tags it Sorting, which is the vocabulary this hub collects.
The sorting guide has the Python template for the pattern and the 401 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 2248. Intersection of Multiple Arrays?
- LeetCode 2248. Intersection of Multiple Arrays is rated Easy on LeetCode.
- What is the time complexity of LeetCode 2248. Intersection of Multiple Arrays?
- The Python solution on this page runs in O(N).
- What is the space complexity of LeetCode 2248. Intersection of Multiple Arrays?
- The Python solution on this page uses O(1000) auxiliary space.
- What topics does LeetCode 2248. Intersection of Multiple Arrays cover?
- LeetCode 2248. Intersection of Multiple Arrays is tagged Array, Hash Table, Counting and Sorting on LeetCode.