Leetcode: Intersection of Two Arrays II

时间:2022-04-15 07:42:49
Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].

Note:
Each element in the result should appear as many times as it shows in both arrays.
The result can be in any order.
Follow up:
What if the given array is already sorted? How would you optimize your algorithm?
What if nums1's size is small compared to nums2's size? Which algorithm is better?
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?

用HashMap

 1 public class Solution {
 2     public int[] intersect(int[] nums1, int[] nums2) {
 3         Map<Integer, Integer> map1 = new HashMap<>();
 4         ArrayList<Integer> arr2 = new ArrayList<>();
 5         for (int elem : nums1) {
 6             if (map1.containsKey(elem)) {
 7                 map1.put(elem, map1.get(elem)+1);
 8             }
 9             else map1.put(elem, 1);
10         }
11         
12         for (int item : nums2) {
13             if (map1.containsKey(item)) {
14                 map1.put(item, map1.get(item)-1);
15                 if (map1.get(item) == 0) map1.remove(item);
16                 arr2.add(item);
17             }
18         }
19         int[] res = new int[arr2.size()];
20         int i = 0;
21         for (Integer each : arr2) {
22             res[i++] = each.intValue();
23         }
24         return res;
25     }
26 }

Follow Up 1: 用two pointer解,可以省存成HashMap

Follow Up 2: 用在长的数组里面binary Search可解

Follow Up 3:

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.

    •  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.