Welcome to Subscribe On Youtube
1213. Intersection of Three Sorted Arrays
Description
Given three integer arrays arr1
, arr2
and arr3
sorted in strictly increasing order, return a sorted array of only the integers that appeared in all three arrays.
Example 1:
Input: arr1 = [1,2,3,4,5], arr2 = [1,2,5,7,9], arr3 = [1,3,4,5,8] Output: [1,5] Explanation: Only 1 and 5 appeared in the three arrays.
Example 2:
Input: arr1 = [197,418,523,876,1356], arr2 = [501,880,1593,1710,1870], arr3 = [521,682,1337,1395,1764] Output: []
Constraints:
1 <= arr1.length, arr2.length, arr3.length <= 1000
1 <= arr1[i], arr2[i], arr3[i] <= 2000
Solutions
Solution 1: Counting
Traverse the three arrays, count the occurrence of each number, then traverse any one of the arrays. If the count of a number is $3$, add it to the result array.
The time complexity is $O(n)$, and the space complexity is $O(m)$. Here, $n$ and $m$ are the length of the array and the range of numbers in the array, respectively.
Solution 2: Binary Search
Traverse the first array. For each number, use binary search to find this number in the second and third arrays. If found in both, add this number to the result array.
The time complexity is $O(n \times \log n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array.
Solution 3: Binary Search
Similar to https://leetcode.ca/2016-11-13-349-Intersection-of-Two-Arrays/ set operation, set1 & set2 & set3
.
>>> set1 = {1, 2, 3}
>>> set2 = {2, 3, 4}
>>> set3 = {3, 4, 5}
>>> set1 & set2 & set3
{3}
>>>
-
class Solution { public List<Integer> arraysIntersection(int[] arr1, int[] arr2, int[] arr3) { List<Integer> ans = new ArrayList<>(); int[] cnt = new int[2001]; for (int x : arr1) { ++cnt[x]; } for (int x : arr2) { ++cnt[x]; } for (int x : arr3) { if (++cnt[x] == 3) { ans.add(x); } } return ans; } }
-
class Solution { public: vector<int> arraysIntersection(vector<int>& arr1, vector<int>& arr2, vector<int>& arr3) { vector<int> ans; int cnt[2001]{}; for (int x : arr1) { ++cnt[x]; } for (int x : arr2) { ++cnt[x]; } for (int x : arr3) { if (++cnt[x] == 3) { ans.push_back(x); } } return ans; } };
-
class Solution: def arraysIntersection( self, arr1: List[int], arr2: List[int], arr3: List[int] ) -> List[int]: return list(set(arr1) & set(arr2) & set(arr3)) ############# class Solution: def arraysIntersection( self, arr1: List[int], arr2: List[int], arr3: List[int] ) -> List[int]: cnt = Counter(arr1 + arr2 + arr3) return [x for x in arr1 if cnt[x] == 3]
-
func arraysIntersection(arr1 []int, arr2 []int, arr3 []int) (ans []int) { cnt := [2001]int{} for _, x := range arr1 { cnt[x]++ } for _, x := range arr2 { cnt[x]++ } for _, x := range arr3 { cnt[x]++ if cnt[x] == 3 { ans = append(ans, x) } } return }
-
class Solution { /** * @param Integer[] $arr1 * @param Integer[] $arr2 * @param Integer[] $arr3 * @return Integer[] */ function arraysIntersection($arr1, $arr2, $arr3) { $rs = []; $arr = array_merge($arr1, $arr2, $arr3); for ($i = 0; $i < count($arr); $i++) { $hashtable[$arr[$i]] += 1; if ($hashtable[$arr[$i]] === 3) { array_push($rs, $arr[$i]); } } return $rs; } }