Leetcode: Intersection of Two Arrays II

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.

转载于:https://www.cnblogs.com/EdwardLiu/p/6097225.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值