【leetcode】Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define a pair (u,v) which consists of one element from the first array and one element from the second array. Find the k pairs (u1,v1),(u2,v2) ...(uk,vk) with the smallest sums. Example 1: Given nums1 = [1,7,11], nums2 = [2,4,6], k = 3 Return: [1,2],[1,4],[1,6] The first 3 pairs are returned from the sequence:
[1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6] Example 2: Given nums1 = [1,1,2], nums2 = [1,2,3], k = 2 Return: [1,1],[1,1] The first 2 pairs are returned from the sequence:
[1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3] Example 3: Given nums1 = [1,2], nums2 = [3], k = 3 Return: [1,3],[2,3] All possible pairs are returned from the sequence:
[1,3],[2,3]
题目的意思很简单,给定两个升序排列的数组,从两个数组中各取一个元素进行配对,并对配对的两个进行求和,找出前K小的配对组合。
最直接的办法就是穷举,把所有的组合都列出来,得到最后的结果,这样的时间复杂度是N^2。那么有没有更简单的方法呢?
由于数组是有序的,那么最小的一个配对组合肯定是 [nums1[0],nums2[0] ],并且 [nums1[i],nums2[j] ] 一定小于 [nums1[i],nums2[j+1] ]。知道这个规律后,次小的配对组合一定是在 [nums1[0],nums2[1] ] ...[nums1[i-1],nums2[0] ], [nums1[i],nums2[0] ]之间产生。假设次小的值是 [nums1[i-1],nums2[0] ],那么第三小的值就是在 [nums1[0],nums2[1] ] ...[nums1[i-1],nums2[1] ], [nums1[i],nums2[0] ]。
所以,我们只需要再维护一个数组A,数组中下标对应nums1的下标,数组对应的值对应nums2的下标。每次只需要循环数组A,计算 nums[X] + nums[A[X]],得到其中的最小值nums[X] + nums[A[X]]就是当前最小配对。找到最小配对后把数组A的A[x] 的值 +1,继续循环,直到找到第K个配对元素为止。
下面是参考代码:
/**
* Created by Administrator on 2016/7/25.
*/
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @param {number} k
* @return {number[][]}
*/
var kSmallestPairs = function(nums1, nums2, k) {
if (nums1.length == 0 || nums2.length == 0){
return []
}
var count = 0;
var list = [];
for (var i = 0;i<nums1.length;i++){
list.push(0)
} var result = []
result.push([nums1[0],nums2[0]])
list[0] = 1
while( k > result.length && result.length < nums1.length*nums2.length){
//console.log(k,result.length)
var minIndex = 0;
var minSum = 0;
var j = 0;
for(j = 0;j<list.length;j++){
if (list[j] < nums2.length){
break
}
}
minSum = nums1[j] + nums2[list[j]]
for(var i = 0;i<list.length;i++){
if(minSum >= nums1[i] + nums2[list[i]]){
minIndex = i
minSum = nums1[i] + nums2[list[i]]
}
}
//console.log(minIndex)
result.push([nums1[minIndex],nums2[list[minIndex]]])
list[minIndex] ++;
}
return result };
【leetcode】Find K Pairs with Smallest Sums的更多相关文章
- #Leetcode# 373. Find K Pairs with Smallest Sums
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/ You are given two integer arrays nums ...
- [LeetCode] 373. Find K Pairs with Smallest Sums 找和最小的K对数字
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...
- 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)
[LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...
- [LeetCode] Find K Pairs with Smallest Sums 找和最小的K对数字
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...
- Leetcode Find K Pairs with smallest sums
本题的特点在于两个list nums1和nums2都是已经排序好的.本题如果把所有的(i, j)组合都排序出来,再取其中最小的K个.其实靠后的很多组合根本用不到,所以效率较低,会导致算法超时.为了简便 ...
- Find K Pairs with Smallest Sums -- LeetCode
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...
- [Swift]LeetCode373. 查找和最小的K对数字 | Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...
- 373. Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. 给你两个数组n ...
- 373. Find K Pairs with Smallest Sums 找出求和和最小的k组数
[抄题]: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. D ...
随机推荐
- golang remote debug和docker debug
在编写 Go 代码的时候,因为很多时候都是需要调试服务器上的代码的,作为一个年长的工程师,肯定不能用 log.Printf 来调试问题,所以我选择了 delve 这个工具,通过 delve 我可以像本 ...
- Python学习之数据库初识
9 数据库 9.1 数据库的初识 数据库是可以独立运行的,并且可以对数据的增删改查提供高效便捷方式的工具. 数据库解决的问题: 解决了操作文件的效率和便捷问题 解决了多个服务同时使用数据时 ...
- 【VS开发】【智能语音处理】DTW算法(语音识别)
DTW主要是应用在孤立词识别的算法,用来识别一些特定的指令比较好用,这个算法是基于DP(动态规划)的算法基础上发展而来的.这里介绍语音识别就先介绍下语音识别的框架,首先我们要有一个比对的模版声音,然后 ...
- python 并发编程 多进程 生产者消费者模型总结
生产者消费者模型总结 生产者消费者模型什么时候用? 1.程序中有两类角色 一类负责生产数据(生产者) 一类负责处理数据(消费者) 2.引入生产者消费者模型为了解决的问题是 平衡生产者与消费者之间的速度 ...
- linux查看cd/dvd驱动器的设备信息
在linux下,如何来查看系统里的CD-ROM或者DVD驱动器的设备名呢? 你可以输入下面的命令来查看当前系统下的光盘驱动器信息: 1.使用dmesg命令来查看当前的硬件是否被linux内核正确的识别 ...
- [转帖]RSA算法与DSA算法的区别
RSA算法与DSA算法的区别 https://cloud.tencent.com/developer/news/254061 文章来源:企鹅号 - SuperFullStack 本文译自:StackE ...
- 思考--PostgreSQL在与mysql的比较中稍微弱势项
PostgreSQL在与mysql的比较中稍微弱势项: 1.都是堆表,没有所谓的聚集索引表,其实问题不大,聚集索引表也只是在使用聚集索引那些列有加速,而且pg也有聚集索引,只不过要定期重建. 2.mv ...
- 初步学习jquery学习笔记(二)
jQuery事件 jquery是为事件处理而设计的 什么是事件? 页面对不同访问者的相应叫做事件. 事件处理程序指的是html中发生某些事件所调用的方法 实例: 在元素上移动鼠标 选取单选按钮 点击元 ...
- 6-4 如何构建xml文档
>>> from xml.etree.ElementTree import Element,ElementTree Element 是节点元素 ElementTree是由 Eleme ...
- NOIP2017 时间复杂度 大模拟
再写一道大模拟题. 由于是限时写的,相当于考场代码,乱的一批. 题目链接:P3952 时间复杂度 先记几个教训: 字符串形式的数字比较大小老老实实写函数,字典序都搞错几次了 栈空的时候不但pop()会 ...