本文共 3965 字,大约阅读时间需要 13 分钟。
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?典型的hash表求解问题!!!
class Solution {public: vector intersect(vector & nums1, vector & nums2) { unordered_mapnums; vector res; int len1=nums1.size(),len2=nums2.size(); if(len1==0||len2==0) return res; for(int i=0;i 0) { nums[nums2[i]]--; res.push_back(nums2[i]); } } return res; }};
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.
/
Let m=nums1.size(), and n=nums2.size()Solution 1: hashtable (using unordered_map).
time complexity: max(O(m), O(n))
space complexity: choose one O(m) or O(n) <— So choose the smaller one if you canvector intersect(vector & nums1, vector & nums2) { if(nums1.size() > nums2.size()) return intersect(nums2, nums1); vector ret; unordered_mapmap1; for(int num:nums1) map1[num]++; for(int num:nums2) { if(map1.find(num)!=map1.end() && map1[num]>0) { ret.push_back(num); map1[num]--; } } return ret;}Solution 2: sort + binary searchtime complexity: max(O(mlgm), O(nlgn), O(mlgn)) or max(O(mlgm),O(nlgn), O(nlgm))O(mlgm) <-- sort first arrayO(nlgn) <--- sort second arrayO(mlgn) <--- for each element in nums1, do binary search in nums2O(nlgm) <--- for each element in nums2, do binary search in nums1space complexity: depends on the space complexity used in yoursorting algorithm, bounded by max(O(m), O(n))vector intersect(vector & nums1, vector & nums2) { vector ret; if(nums1.empty() || nums2.empty()) return ret; sort(nums1.begin(), nums1.end()); sort(nums2.begin(), nums2.end()); int j=0; for(int i=0; i
So if two arrays are already sorted, and say m is much smaller than n,
we should choose the algorithm that for each element in nums1, do binary search in nums2, so that the complexity is O(mlgn). In this case, if memory is limited and nums2 is stored in disk, partition it and send portions of nums2 piece by piece. keep a pointer for nums1 indicating the current position, and it should be working fine~转载地址:http://lexvi.baihongyu.com/