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. 【NOIP】提高组2013 火柴排队

    [题意]两列n个火柴,分别有高度ai和bi(同一列高度互不相同),每次可以交换一列中的两个相邻火柴,定义距离为∑(ai-bi)^2,求使距离最小的最少交换次数,n<=10^5. [算法]逆序对 ...

  2. JS之document例题讲解2

    例题三.图片轮播 <body> <div style="width:1000px; height:250px; margin-top:30px"> < ...

  3. Achain 钱包部署

    官网 GIT: [ Achain_linux ] 基础环境 OS: CentOS, Ubuntu Achain: 官网 [ release 最新版本 ] 安装 Achain 钱包 下载 CentOS ...

  4. 从C语言项目谈编程

    很多初学C语言的小伙伴,在学习之初并没有一个大概的概念,学习这门语言需要掌握多少知识点,怎么才算学的差不多? C语言的精髓点在哪? 学到多少东西才能够达到做项目的标准?学习的时候需要注意哪些细节点?疑 ...

  5. css纯样式导航

    <style>.dropdown {    position: relative;    display: inline-block;} .dropdown-content {    di ...

  6. 头像截取 图片上传 js插件

    先看一下整体效果 页面html <div class="row"> <div class="tabs-container"> <u ...

  7. dokuwiki安装部署

    dokuwiki的地址:https://www.dokuwiki.org/dokuwiki# 1.部署dokuwiki 在D:\xampp\htdocs(xampp安装目录)新建一个doku文件夹,把 ...

  8. 制作Solaris系统的USB启动盘

    制作方法: 1. wget http://192.168.2.5/surefiler-installer/2011-12-09/devel-2011.12.9.tgz 2. cd /root tar  ...

  9. H题 hdu 2520 我是菜鸟,我怕谁

    题目大意:http://acm.hdu.edu.cn/showproblem.php?pid=2520 我是菜鸟,我怕谁 Time Limit: 2000/1000 MS (Java/Others)  ...

  10. 基于Django Form源码开发自定义Form组件

    import copy import re class ValidateError(Exception): def __init__(self, detail): self.detail = deta ...