[Algorithm] 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]Note:
- Each element in the result should appear as many times as it shows in both arrays.
- The result can be in any order.
Not good enough approach
var intersect = function(nums1, nums2) {
let r = [];
// get larger array
const len1 = nums1.length;
const len2 = nums2.length; // larger & smaller
const larger = len1 > len2 ? nums1: nums2;
const smaller = len1 > len2 ? nums2: nums1; // conver larger array to object
let hashed = {};
for (let n of larger) {
if (n in hashed) {
hashed[n]++;
} else {
hashed[n] = 1;
}
} // loop over smaller array
for (let n of smaller) {
if (`${n}` in hashed) {
r.push(n);
hashed[n] = hashed[n]-1;
if (hashed[n] === 0) {
delete hashed[n];
}
}
} return r;
};
The reason that code above is not good enough is because, \
1. we use larger array as lookup, this cause more memory usage. -- actually we need to use smaller array as lookup
2. we use 'len1, len2, samller, larger' extra storage, we can actully swap nums1 and nums by one extra function call.
var intersect = function(nums1, nums2) { if (nums1.length > nums2.length) {
return intersect(nums2, nums1);
} // conver samller array to object
let hashed = {};
for (let n of nums2) {
if (n in hashed) {
hashed[n]++;
} else {
hashed[n] = 1;
}
} let r = [];
// loop over smaller array
for (let n of nums1) {
if (hashed[n] > 0) {
r.push(n);
hashed[n] = hashed[n]-1;
}
} return r;
}
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?
Will write another post for the follow up questions.
[Algorithm] 350. Intersection of Two Arrays II的更多相关文章
- 26. leetcode 350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...
- 【leetcode】350. Intersection of Two Arrays II
problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...
- [LeetCode] 350. Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- 【一天一道LeetCode】#350. Intersection of Two Arrays II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- [LeetCode] 350. Intersection of Two Arrays II 两个数组相交II
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- leetcode349 350 Intersection of Two Arrays & II
""" Intersection of Two Arrays Given two arrays, write a function to compute their in ...
- LeetCode 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
随机推荐
- FusionInsight大数据开发---Kafka应用开发
Kafka应用开发 了解Kafka应用开发适用场景 熟悉Kafka应用开发流程 熟悉并使用Kafka常用API 进行Kafka应用开发 Kafka的定义Kafka是一个高吞吐.分布式.基于发布订阅的消 ...
- Java的内存需要划分成为5个部分:
Java的内存需要划分成为5个部分: 1.栈(Stack):存放的都是方法中的局部变量.方法的运行一定要在栈当中运行. 局部变量:方法的参数,或者是方法{}内部的变量 作用域:一旦超出作用域,立从栈内 ...
- UDP比TCP好用的优势
网络带宽环境变好 在2007年至2015年间,网络的带宽飞速发展,从1.5Mbps的带宽增加到5.1Mbps的带宽,足足增加了4倍,网络环境快速.稳定,所以UDP的丢包率 下降至5%以下,越来越好的网 ...
- Java之四大元注解@Target、@Retention、@Documented、@Inherited
什么叫做元注解?? ==>用于注解[注释]的注解就叫做元注解 注解叫做:元数据,标签,注释 元注解[数据]--->注解--->标记代码 1.@Target : ...
- c#winform简单实现Mysql数据库的增删改查的语句
通过简单的SQL语句实现对数据库的增删改查. 窗口如下: 定义打开与关闭连接函数,方便每次调用: 增加指令: 删除指令: 修改指令: 查找指令: 表格情况:
- SQL server中常用sql语句
--循环执行插入10000条数据 declare @ID intbeginset @ID=1 while @ID<=10000begininsert into table_name values ...
- 理解 Kubernetes 的亲和性调度
这次给大家介绍下k8s的亲和性调度:nodeSelector.nodeAffinity.podAffinity.Taints以及Tolerations用法. 一般情况下我们部署的 POD 是通过集群自 ...
- Mycat分布式数据库架构解决方案--搭建MySQL读写分离环境--一主多从
echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!!! 本文主 ...
- JS调用栈的一些总结
原文地址 调用栈 调用栈是解释器追踪函数执行流的一种机制.当执行环境中调用了多个函数函数时,通过这种机制,我们能够追踪到哪个函数正在执行,执行的函数体中又调用了哪个函数. 我们知道JavaScript ...
- asp.net 自定义特性
今天看张子阳的.net中的反射(反射特性)一文,觉得反射配合自定义的特性确实还挺有用,之前看书.看博客之后好多心血来潮敲的代码随便往桌面上一放,时间一久,连自己也分不清它们是干嘛的了,然后就是删除,虽 ...