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 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 = [,,], nums2 = [,,], k = Return: [,],[,],[,] The first pairs are returned from the sequence:
[,],[,],[,],[,],[,],[,],[,],[,],[,]
Example 2:
Given nums1 = [,,], nums2 = [,,], k = Return: [,],[,] The first pairs are returned from the sequence:
[,],[,],[,],[,],[,],[,],[,],[,],[,]
Example 3:
Given nums1 = [,], nums2 = [], k = Return: [,],[,] All possible pairs are returned from the sequence:
[,],[,]
思路:这个题目需要转化一下。我们构建一个二维矩阵matrix,matrix[i][j]=nums1[i] + nums2[j]。
例如:对于nums1=[1, 7, 11], nums2=[2, 4, 6],矩阵matrix是这样子:
+------------
|
|
|
因为nums1和nums2都是有序的,因此matrix里每一行都是从小到大,每一列也是从小到大。
那么这个题就变成了,在这个矩阵中找前k小的数。我们用最小堆可以解决这个问题。
首先,左上角的matrix[0][0]肯定是最小的。我们将它放入堆中,作为seed。
之后,我们对这个堆做K次操作:
- 从堆顶取出最小的数,判断它在矩阵中的行和列(可以用tuple实现),将对应的nums1和nums2的两个数构造成pair添加进结果。
- 若取出的数不在矩阵最后一列,则将该行它的下一个数放入堆中。
- 若取出的数在矩阵第一列,且不在最后一行,则还要将它的下一行行首的数放入堆中。
- 若堆为空,则提前退出循环(没有这么多pair)。
算法复杂度: K次循环,每次循环从堆中取出一个数,最多放入两个数,则堆空间最大为O(K), push和pop操作复杂度为O(logK)。总时间复杂度为O(KlogK)。
代码心得:tuple类型声明往往比较长,可以用typedef。
class Solution {
public:
vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
vector<pair<int, int> > res;
if (!nums1.size() || !nums2.size()) return res;
typedef tuple<int, int, int> triInt;
vector<triInt> heap(, make_tuple(nums1[] + nums2[], , ));
int height = nums1.size(), width = nums2.size();
while (k-- && heap.size()) {
triInt top = heap.front();
std::pop_heap(heap.begin(), heap.end(), greater<triInt>()); heap.pop_back();
int row = get<>(top), col = get<>(top);
res.push_back(make_pair(nums1[row], nums2[col]));
if (col < width - ) {
heap.push_back(make_tuple(nums1[row] + nums2[col + ], row, col + ));
std::push_heap(heap.begin(), heap.end(), greater<triInt>());
}
if (col == && row < height - ) {
heap.push_back(make_tuple(nums1[row + ] + nums2[col], row + , col));
std::push_heap(heap.begin(), heap.end(), greater<triInt>());
}
}
return res;
}
};
Find K Pairs with Smallest Sums -- LeetCode的更多相关文章
- 【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# 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 Find K Pairs with smallest sums
本题的特点在于两个list nums1和nums2都是已经排序好的.本题如果把所有的(i, j)组合都排序出来,再取其中最小的K个.其实靠后的很多组合根本用不到,所以效率较低,会导致算法超时.为了简便 ...
- 【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 ...
- [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 ...
随机推荐
- 【51NOD-0】1008 N的阶乘 mod P
[算法]简单数学 [题解]多项式展开:(a*b)%p=(a%p*b%p)%p #include<cstdio> #include<algorithm> #define rep( ...
- [Unity]插件Node Editor介绍 实现类似状态机画布的扩展
Unity自带的动画状态机有一套对策划非常友好的UI.但是Unity官方没有公开这些控件的api.除了Asset Store里一些已有的方案,我在这里介绍一个在github上的开源项目,封装了底层,但 ...
- spring项目中web-inf下不能引用页面资源
1.spring项目结构 2.spring结构说明 web-inf目录是不对外开放的,外部没办法直接访问到(即通过url访问),只有通过映射来访问,如映射一个action或servlet通过服务器端跳 ...
- pcap的安装
pcap,即 packet capture library 抓包库,这个抓包库给抓包系统提供了一个高层次的接口.所有网络上的数据包,甚至是那些发送给其他主机的,通过这种机制,都是可以捕获的.它也支持把 ...
- GDB实战
程序中除了一目了然的Bug之外都需要一定的调试手段来分析到底错在哪.到目前为止我们的调试手段只有一种:根据程序执行时的出错现象假设错误原因,然后在代码中适当的位置插入 printf ,执行程序并分析打 ...
- Perl中文件读取操作
Perl中文件读取操作 http://blog.csdn.net/yangxuan12580/article/details/51506216
- php 读写 csv文件
读取csv function input_csv($handle) { $out = array (); $n = 0; while ($data = fgetcsv($handle, 10000)) ...
- centos创建子用户
#adduser 用户名 #passwd 用户名 设置自己的密码即可 授权 个人用户的权限只可以在本home下有完整权限,其他目录要看别人授权.而经常需要root用户的权限,这时候sudo可以化身为r ...
- IntelliJ IDEA 启动方法
IntelliJ IDEA cd idea-IU-145.1617.8/bin && ./idea.sh
- Python+Selenium 自动化实现实例-打开浏览器模拟进行搜索数据并验证
#导入模块 from selenium import webdriverfrom selenium.webdriver.common.keys import Keys #启动火狐浏览器driver = ...