【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 ...
随机推荐
- 简单通讯聊天 群聊功能 Windows下的客户端 Linux下的epoll服务器
1 服务器代码 Linux eclipse C++ //======================================================================= ...
- LeetCode.965-单一二叉树(Univalued Binary Tree)
这是悦乐书的第366次更新,第394篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第228题(顺位题号是965).如果树中的每个节点具有相同的值,则二叉树是单一的.当且仅 ...
- 11.8 Springcloud项目简介
各位领导好,我从毕业后做了两年Java开发工程师,刚开始都是一些SSM框架的项目,但是由于技术不断更新,微服项目成为必然的趋势,大约在做了1年的SSM框架,之后开始接触微服项目,前后经理过Dubbo和 ...
- 如何理解springcloud微服务项目中,eureka,provider,consumer它们之间的关系?
eureka负责注册provider和consumer的服务信息 provider负责与数据库进行交互,实现数据持久化,并给consumer提供服务 consumer与前端交互,通过与Eureka同源 ...
- 服务器上安装并使用tensorboard
需求: 在ubunu16.0的服务器上使用Pytorch内嵌的tensorboard 安装 pip install tensorflow pip install tensorboardX 如果嫌慢可以 ...
- 【VS开发】使用WinPcap编程(2)——打开网络设备并且开始捕获数据包
这里需要特别强调的一个数据结构是pcap_t,它相当于一个文件描述符,代表一个已经打开的设备.我们对这个设备进行操作,就是对这个文件描述符进行操作. 首先是打开一个已知的设备,使用pcap_open( ...
- PTA(Basic Level)1010.一元多项式求导
设计函数求一元多项式的导数.(注:\(x^n\)(\(n\)为整数)的一阶导数为\(nx^{n−1}\).) 输入格式: 以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过 1000 的整数) ...
- [转帖]2018年全球ERP软件行业市场规模与发展趋势分析 云ERP将兴起【组图】
2018年全球ERP软件行业市场规模与发展趋势分析 云ERP将兴起[组图] https://www.qianzhan.com/analyst/detail/220/190215-4b1d6868.ht ...
- 19.AutoMapper 之开放式泛型(Open Generics)
https://www.jianshu.com/p/ce4c7e291408 开放式泛型(Open Generics) AutoMapper可以支持开放式泛型的映射.为开放式泛型创建映射: publi ...
- docker常用技巧
1:运行中容器如何保存为一个镜像? docker commit 容器名字 镜像名字 2:怎么给容器增加名字 docker rename 容器id(或名字)name(新名字) 3:docker中的Doc ...