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 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 = [1,7,11], nums2 = [2,4,6], k = 3 Return: [1,2],[1,4],[1,6] The first 3 pairs are returned from the sequence:
[1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
Example 2:
Given nums1 = [1,1,2], nums2 = [1,2,3], k = 2 Return: [1,1],[1,1] The first 2 pairs are returned from the sequence:
[1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
Example 3:
Given nums1 = [1,2], nums2 = [3], k = 3 Return: [1,3],[2,3] All possible pairs are returned from the sequence:
[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)是否小于堆顶的元素。

def kSmallestPairs(nums1, nums2, k):
"""
:type nums1: List[int]
:type nums2: List[int]
:type k: int
:rtype: List[List[int]]
"""
ans = []
heap = [(0x7FFFFFFF, None, None)]
size1, size2 = len(nums1), len(nums2)
idx2 = 0
while len(ans) < min(k, size1 * size2):
if idx2 < size2:
sum, i, j = heap[0]
if nums2[idx2] + nums1[0] < sum:
for idx1 in range(size1):
heapq.heappush(heap, (nums1[idx1] + nums2[idx2], idx1, idx2))
idx2 += 1
sum, i, j = heapq.heappop(heap)
ans.append((nums1[i], nums2[j]))
return ans
Leetcode Find K Pairs with smallest sums的更多相关文章
- [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 解题报告(Python)
[LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...
- #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 ...
- 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 ...
- 【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 ...
随机推荐
- noi题库(noi.openjudge.cn) 1.8编程基础之多维数组T01——T10
T01 矩阵交换行 描述 给定一个5*5的矩阵(数学上,一个r×c的矩阵是一个由r行c列元素排列成的矩形阵列),将第n行和第m行交换,输出交换后的结果. 输入 输入共6行,前5行为矩阵的每一行元素,元 ...
- springmvc 通过异常增强返回给客户端统一格式
在springmvc开发中,我们经常遇到这样的问题:逻辑正常执行时返回客户端指定格式的数据,比如json,但是遇NullPointerException空指针异常,NoSuchMethodExcept ...
- struts2 访问Web元素的4种方法
完整代码 :Struts12AccessWebElement.rar 第一种也是最常用的一种方法实现这几个接口 RequestAware,SessionAware,ApplicationAware s ...
- 每一个C#开发者必须知道的13件事情
1.开发流程 程序的Bug与瑕疵往往出现于开发流程当中.只要对工具善加利用,就有助于在你发布程序之前便将问题发现,或避开这些问题. 标准化代码书写 标准化代码书写可以使代码更加易于维护,尤其是在代码由 ...
- Euler Level 2
闲下来的时候就做点,慢慢的也终于到达Level 2了.
- 20151120 - 蓝牙鼠标与 WiFi 冲突的解决办法
问题现象描述:Windows 下蓝牙鼠标移动时不连贯 电脑:Dell 2015 版 NEW XPS 15 鼠标:Microsoft Bluetooth Designer Mouse 操作系统:Wind ...
- HTTP请求头参数
Accept-Language: zh-cn,zh;q=0.5 意思:浏览器支持的语言分别是中文和简体中文,优先支持简体中文. 详解: Accept-Language表示浏览器所支持的语言类型: ...
- 网络设计中需要考虑的时延latency差异
Jeff Dean提到不同数据访问方式latency差异 Numbers Everyone Should Know L1 cache reference 0.5 ns Branch mispredic ...
- nginx 编译参数详解(运维不得不看)
nginx参数: --prefix= 指向安装目录 --sbin-path 指向(执行)程序文件(nginx) --conf-path= 指向配置文件(nginx.conf) --error-log- ...
- jsonp的三种跨域方式
1.通过jq的$.ajax()完成跨域,这是我比较喜欢的一种方式. 代码如下: $.ajax({ type:'get', async:true, url:'地址', dataType:'jsonp', ...