博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode——350. Intersection of Two Arrays II
阅读量:4136 次
发布时间:2019-05-25

本文共 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_map
nums; 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 can

vector
intersect(vector
& nums1, vector
& nums2) { if(nums1.size() > nums2.size()) return intersect(nums2, nums1); vector
ret; unordered_map
map1; 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
& nums, int target) { int l=0, r=nums.size()-1; while(l

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/

你可能感兴趣的文章
JavaScript深入理解之闭包
查看>>
这才是学习Vite2的正确姿势!
查看>>
7 个适用于所有前端开发人员的很棒API,你需要了解一下
查看>>
25个构建Web项目的HTML建议,你需要了解一下!
查看>>
【web素材】02-10款大气的购物商城网站模板
查看>>
6种方式实现JavaScript数组扁平化(flat)方法的总结
查看>>
如何实现a===1 && a===2 && a===3返回true?
查看>>
49个在工作中常用且容易遗忘的CSS样式清单整理
查看>>
20种在学习编程的同时也可以在线赚钱的方法
查看>>
隐藏搜索框:CSS 动画正反向序列
查看>>
12 个JavaScript 特性技巧你可能从未使用过
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(上)
查看>>
【视频教程】Javascript ES6 教程27—ES6 构建一个Promise
查看>>
【5分钟代码练习】01—导航栏鼠标悬停效果的实现
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(中)
查看>>
8种ES6中扩展运算符的用法
查看>>
【视频教程】Javascript ES6 教程28—ES6 Promise 实例应用
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(下)
查看>>
【web素材】03-24款后台管理系统网站模板
查看>>
Flex 布局教程:语法篇
查看>>