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. 【LibreOJ】#538. 「LibreOJ NOIP Round #1」数列递推

    [题意]LibreOJ [算法]乱搞 [题解]容易发现数列最后一定单调,最后单调递增则最大值赋为最后一个,反之最小值赋为最后一个,然后处理一些细节就可以AC,要注意以下几点: 1.数列连续三项以及数列 ...

  2. HDU 2059 龟兔赛跑 (dp)

    题目链接 Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击--赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州下沙某农业园卧薪尝胆潜心修炼,终于练成 ...

  3. flask函数已定义参数却出现takes 0 positional arguments but 1 was given的问题

    在flask中定义了一个简单的删除数据库内容的路由 测试却发现一直报错 说delete_history函数定义时没有接受参数,但是检查delete_history函数却发现没有问题 后来想了半天才发现 ...

  4. bzoj 2165 DP

    首先如果不考虑数据范围的话,因为每一层都是等效的,所以我们可以用w[i][j][k]来表示在某一层的j位置,称作i次电梯到k位置,最多上升多少层,那么我们可以比较容易的写出转移,因为m十分大,i可能与 ...

  5. js_参数的get传输,从一个页面到另外一个页面。

    2017年10月31日,今天是万圣节,欢乐谷搞事情. 刚接触前端那会是分不清,前端和后台的,后台的数据如何传输到前端的. 现在用的还是Jquery的ajax请求后台数据到前端页面的,需要学习的地方还有 ...

  6. Html5_sessionStrong和localStorage的灵活使用

    谈谈这两个属性sessionStrong和localStorage是Html5新增点属性,用来记录一些数据在浏览器. 两者的区别sessionStrong存储的数据是暂时的,浏览器关掉后,存储下来的数 ...

  7. html中的meta标签

    1.定义 meta元素提供页面的原信息,位于文档头部 2.必须的属性 content属性 该属性提供名称/值对中的值,使用要与http-equiv或name属性一起使用 3.可选的属性 3.1.htt ...

  8. Python模块学习 - Functools

    Functools模块 Higher-order functions and operations on callable objects,看这个标题我都是懵逼的,这都是啥啥啥啊,赶紧拿出百度翻译:可 ...

  9. python模块 zipfile

    zipfile是python里用来做zip格式编码的压缩和解压缩的,由于是很常见的zip格式,所以这个模块使用频率也是比较高的zipfile里有两个非常重要的class, 分别是ZipFile和Zip ...

  10. [Leetcode Week13]Search a 2D Matrix

    Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...