【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 ...
随机推荐
- 基于 vue2 + vuex 构建一个具有 45 个页面的大型单页面应用
源码地址: https://github.com/bailicangdu/vue2-elm 技术栈 vue2 + vuex + vue-router + webpack + ES6/7 + fetch ...
- Vim命令合集(四)
Vim命令合集 命令历史 以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中输入以下命令即可 vim 直接启动vim vim filena ...
- linux top 查看CPU命令
top 命令主要用于查看进程的相关信息,同时它也会提供查看系统平均负载,cpu 信息和内存信息 实时监控系统资源使用情况 [root@localhost ~]$ top // 动态查看进程使用资源的情 ...
- effective_io_concurrency很重要的一个参数
effective_io_concurrency (integer) Sets the number of concurrent disk I/O operations that PostgreSQL ...
- Tomcat部署SSL证书过程中遇到的问题
在CentOS7中Tomcat部署SSL证书时遇到的问题 1.配置servlet.xml 配置tomcat/conf/server.xml 加入以下代码 <Connector port=&quo ...
- jupyter notebook 使用多个python环境
conda install nb_conda_kernels 执行上面的命令,然后启动notebook就可以选择conda中的所有环境了
- iter的特殊用法以及偏函数partial
iter()的特殊用法 常规使用 iter(obj),会返现一个迭代器,如果 obj 不是可迭代对象,则会报错. 特殊用法(哨兵模式) iter(object[, sentinel]) sentine ...
- python 元类 MetaClass
type() 动态语言和静态语言最大的不同,就是函数和类的定义,不是编译时定义的,而是运行时动态创建的. 比方说我们要定义一个Hello的class,就写一个hello.py模块: class Hel ...
- Vue中的组件直接的通信是如何实现的
组件关系可分为父子组件通信.兄弟组件通信 1.父组件传子组件 通过props属性来实现 2.子组件传父组件 子组件用$emit()来触发事件,父组件用$on()来监听子组件的事件 3.兄弟之间的通信 ...
- 如何写一个 Burp 插件
Burp 是 Web 安全测试中不可或缺的神器.每一个师傅的电脑里面应该都有一个 Burp.同时 Burp 和很多其他神器一样,它也支持插件.但是目前总体来说网上 Burp 插件开发的资料不是特别特别 ...