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的更多相关文章

  1. 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)

    [LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...

  2. [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 ...

  3. #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 ...

  4. [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 ...

  5. Leetcode Find K Pairs with smallest sums

    本题的特点在于两个list nums1和nums2都是已经排序好的.本题如果把所有的(i, j)组合都排序出来,再取其中最小的K个.其实靠后的很多组合根本用不到,所以效率较低,会导致算法超时.为了简便 ...

  6. 【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 ...

  7. [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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. UIScrollView---iOS-Apple苹果官方文档翻译

      本系列所有文章,链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址(2013年12月29日更新版) //转载请注明出处--本文永久链接:http://www ...

  2. js 重置表单

    //方法一document.getElementById("myform").reset(); //方法二 ].reset(); //方法三 使用input按钮 <input ...

  3. Python 编码问题:出现中文乱码-- (转)

    问题描述: 在写Python代码的过程中,有用到需要输出中文的地方(python2.6.5在中文注释的地方就会出错),但是运行后会出错 我的错误显示: SyntaxError: Non-ASCII c ...

  4. Eureka服务下线(Cancel)源码分析

    Cancel(服务下线) 在Service Provider服务shut down的时候,需要及时通知Eureka Server把自己剔除,从而避免其它客户端调用已经下线的服务,导致服务不可用. co ...

  5. 【转】gif文件格式详解

    1.概述 ~~~~~~~~ GIF(Graphics Interchange Format,图形交换格式)文件是由 CompuServe公司开发的图形文件格式,版权所有,任何商业目的使用均须 Comp ...

  6. web服务器和数据库服务器不在一台机器上

    把localhost改成数据库所在的IP就行了. $link=mysql_connect( "202.195.246.202 ", "root ", " ...

  7. nsa工程式(fb.py): perl6调用并修改IP

    use v6; if (@*ARGS != 1) {say 'Use:scan.p6 ip';exit;} my $check_ip = @*ARGS[0]; $check_ip = '<val ...

  8. 【LOJ2254】SNOI2017一个简单的询问

    莫队,每次询问的是两个区间,就把区间拆开,分开来算就好了. 借鉴了rank1大佬的玄学排询问的姿势. #include<bits/stdc++.h> #define N 50010 typ ...

  9. OC 06 Block、数组高级

    主要内容: ⼀.Block语法 ⼆.Block使⽤ 三.Block实现数组排序 Block简介 Block:块语法,本质上是匿名函数(没有名称的函数) 标准C⾥面没有Block,C语⾔言的后期扩展版本 ...

  10. pom报错解决方法大全

    1.Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom 解决方法: Windows: CMD --> c ...