350.Intersection of Two Arrays II
1-HashMap (O(M+N))
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
HashMap<Integer, Integer> map = new HashMap<>();
List<Integer> ansList = new ArrayList<>();
for(int i = 0; i < nums1.length; i++){
map.put(nums1[i], map.getOrDefault(nums1[i], 0) + 1);
}
for(int i = 0; i < nums2.length; i++){
if(map.containsKey(nums2[i]) && map.get(nums2[i]) > 0){
ansList.add(nums2[i]);
map.put(nums2[i], map.get(nums2[i]) - 1);
}
}
int[] ans = new int[ansList.size()];
for(int i = 0; i < ansList.size(); i++){
ans[i] = ansList.get(i);
}
return ans;
}
}
2-Two Pointer + Sort
O(n*log n + m*log m)
time complexity.
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
List<Integer> res = new ArrayList<>();
Arrays.sort(nums1);
Arrays.sort(nums2);
for(int i = 0, j = 0; i < nums1.length && j < nums2.length; ){
if(nums1[i] < nums2[j]){
i++;
}
else if(nums1[i] == nums2[j]){
res.add(nums1[i]);
i++;
j++;
}
else{
j++;
}
}
int[] ans = new int[res.size()];
for(int i = 0; i < res.size(); i++){
ans[i] = res.get(i);
}
return ans;
}
}
3-Follow Up
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/82243/
What if elements of nums2 are stored on disk, and the memory is
limited such that you cannot load all elements into the memory at
once?
If only nums2 cannot fit in memory, put all elements of nums1 into a HashMap, read chunks of array that fit into the memory, and record the intersections.
If both nums1 and nums2 are so huge that neither fit into the memory, sort them individually (external sort), then read 2 elements from each array at a time in memory, record intersections.
Thanks for the solution. I think the second part of the solution is impractical, if you read 2 elements at a time, this procedure will take forever. In principle, we want minimize the number of disk access during the run-time.
An improvement can be sort them using external sort, read (let’s say) 2G of each into memory and then using the 2 pointer technique, then read 2G more from the array that has been exhausted. Repeat this until no more data to read from disk.
But I am not sure this solution is good enough for an interview setting. Maybe the interviewer is expecting some solution using Map-Reduce paradigm.
I think the goal of this question is to test whether the interview understands some of the data engineering techniques. From a data engineer’s perspective, basically there are three ideas to solve the question:
Store the two strings in distributed system(whether self designed or not), then using_MapReduce_technique to solve the problem;
Processing the Strings bychunk, which fits the memory, then deal with each chunk of data at a time;
Processing the Strings bystreaming, then check.
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/82271/
Q. What if the given array is already sorted? How would you optimize your algorithm?
If both arrays are sorted, I would use two pointers to iterate, which somehow resembles the merge
process in merge sort.
Q. What if nums1’s size is small compared to nums2’s size? Which algorithm is better?
Suppose lengths of two arrays areN
andM
, the time complexity of my solution isO(N+M)
and the space complexity ifO(N)
considering the hash. So it’s better to use the smaller array to construct the counter hash.
Well, as we are calculating the intersection of two arrays, the order of array doesn’t matter. We are totally free to swap to arrays.
Q. What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
Divide and conquer. Repeat the process frequently: Slice nums2
to fit into memory, process (calculate intersections), and write partial results to memory.