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:

  1. Given nums1 = [1,7,11], nums2 = [2,4,6], k = 3
  2.  
  3. Return: [1,2],[1,4],[1,6]
  4.  
  5. The first 3 pairs are returned from the sequence:
  6. [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]

Example 2:

  1. Given nums1 = [1,1,2], nums2 = [1,2,3], k = 2
  2.  
  3. Return: [1,1],[1,1]
  4.  
  5. The first 2 pairs are returned from the sequence:
  6. [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]

Example 3:

  1. Given nums1 = [1,2], nums2 = [3], k = 3
  2.  
  3. Return: [1,3],[2,3]
  4.  
  5. All possible pairs are returned from the sequence:
  6. [1,3],[2,3]

本题的特点在于两个list nums1和nums2都是已经排序好的。本题如果把所有的(i, j)组合都排序出来,再取其中最小的K个。其实靠后的很多组合根本用不到,所以效率较低,会导致算法超时。为了简便,把nums1和nums2里面的元素写成A1,A2,...,A5, B1,...,B5. 维护一个最小堆。并逐渐往里面push and pop元素。

注意以下几点:1. 如果(Ai, Bj)大于堆顶的元素,那么没有必要把(Ai, Bj)和它之后的元素Push进堆。2. 为了便于管理,解法里其实只维护了一个指针idx2。如果 (A1,B[idx2])已经小于堆顶的元素,那么就把(A1,B[idx2]),...,(A5,B[idx2])全都推到堆里面。

第〇步,把一个最大元素放进堆。注意这一步很重要,否则可能会出现(A1,B2)大于所有(Ai,B1)的情况,也就是第二步中红色箭头大于蓝色箭头的情况始终不会出现,那么如果k足够大,就可能会出现pop一个空堆的bug。

第一步 把所有的(A1,B1),..., (A5,B1)推入堆。也就是把所有的黑色箭头连接的pair都放到堆里。

然后在逐个pop堆顶的元素。直到出现(A1,B2)小于堆顶的情况 (红色箭头小于蓝色箭头)。如果出现,把所有的(A1,B2),...,(A5,B2)放进堆。恢复pop堆顶元素,并且开始监视下一个红色箭头(A1,B3)是否小于堆顶的元素。

  1. def kSmallestPairs(nums1, nums2, k):
  2. """
  3. :type nums1: List[int]
  4. :type nums2: List[int]
  5. :type k: int
  6. :rtype: List[List[int]]
  7. """
  8. ans = []
  9. heap = [(0x7FFFFFFF, None, None)]
  10. size1, size2 = len(nums1), len(nums2)
  11. idx2 = 0
  12. while len(ans) < min(k, size1 * size2):
  13. if idx2 < size2:
  14. sum, i, j = heap[0]
  15. if nums2[idx2] + nums1[0] < sum:
  16. for idx1 in range(size1):
  17. heapq.heappush(heap, (nums1[idx1] + nums2[idx2], idx1, idx2))
  18. idx2 += 1
  19. sum, i, j = heapq.heappop(heap)
  20. ans.append((nums1[i], nums2[j]))
  21. return ans

Leetcode Find K Pairs with smallest sums的更多相关文章

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

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

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

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

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

    本文分享几个php header函数的例子,有需要的朋友参考学习下. 转自:http://www.jbxue.com/article/php_header_x5hV63c.html 1,可以使用hed ...

  2. CSS3中的Rem值与Px之间的换算

    bootstrap默认 html{font-size: 10px;} rem是一个相对大小的值,它相对于根元素<html>, 比如假设,我们设置html的字体大小的值为html{font- ...

  3. 解决ssh-connect-to-host-github-com-port-22-connection-timed-out

    PC:~$ ssh git@github.com ssh: connect to host github.com port 22: Connection timed out 解决办法:(linux下) ...

  4. QT 数据库编程三

    //mainwindow.cpp #include "mainwindow.h" #include "logindlg.h" #include "sc ...

  5. VMware Fusion 中如何复制centos/linux虚拟机

    今天想在mac本上,弄几个centos的虚拟机,尝试搭建hadoop的全分布环境.一台台虚拟机安装过去太麻烦了,想直接将现有的centos虚拟机复制几份完事,但是复制出来的虚拟机无法上网,折腾了一翻, ...

  6. DispatcherHelper

    DispatcherHelper 通常,WPF 应用程序从两个线程开始:一个用于处理呈现, 一个用于管理 UI.呈现线程有效地隐藏在后台运行,而 UI 线程则接收输入.处理事件.绘制屏幕 以及运行应用 ...

  7. 小记sql server临时表与表变量的区别

    临时表与表变量都可以起到“临时”的作用,那么两者主要的区别是什么呢? 这里不讨论创建方式,以及全局临时表.会话临时表这些,主要记录一下个人对两者的主要区别以及适用情况的看法,有什么不对或补充的地方,欢 ...

  8. c++虚函数注意事项

    >在基类方法声明中使用关键字virtual,可以使该方法在基类及所有的派生类中是虚的 >如果使用指向对象的引用或指针来调用虚方法,程序将使用对象类型定义的方法,而不使用为引用或指针类型定义 ...

  9. Spring MVC 急速集成 Shiro 实录

    相信有很多的程序员,不愿意进行用户管理这块代码实现. 原因之一,不同的JavaEE 系统,用户管理都会有个性化的实现,逻辑很繁琐. 而且是系统门面,以后背锅的几率非常大,可谓是低收益高风险. 最近在系 ...

  10. sql中去除重复的项

    方法一:group by  (取最小的id)select min(id) id,T from Table_1 group by T 方法二:union (不需要id)select T from Tab ...