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

标签: LeetCode


题目地址:https://leetcode.com/problems/find-k-pairs-with-smallest-sums/description/

题目描述:

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]

题目大意

从两个数组中各拿出一个数字,求两个数字的和最小的k个组合。如果和相等的时候,出现的顺序是不在乎的。

解题方法

看到全排列,于是我用了笛卡尔积,结果遇到特别长的两个数组时,内存超了。。代码还是很简单的。

class Solution(object):
def kSmallestPairs(self, nums1, nums2, k):
"""
:type nums1: List[int]
:type nums2: List[int]
:type k: int
:rtype: List[List[int]]
"""
pairs = list(itertools.product(nums1, nums2))
return sorted(pairs, key = lambda x: sum(x))[:k]

方法二:

使用堆。说实话,这个堆的使用我是感觉真是不令人满意啊。我们一般用这种数据结构都是把结构里的内容正好用完的。这个题中堆的使用很随意,也是直接把“可能成为和最小的一组数”直接进堆,不用管这个数字是不是真的是最小的k个之一。

注意,不用保证每次进堆的元素是和最小!相对较小即可!

首先将(nums1[i] + nums2[0], i, 0)加入堆,i取值范围[0, size1)

弹出堆顶元素sum, i, j,将(nums1[i], nums2[j])加入结果集ans

若j + 1 < size2,则将(nums1[i] + nums2[j + 1], i, j + 1)加入堆

循环直到结束

参考:http://bookshadow.com/weblog/2016/07/07/leetcode-find-k-pairs-with-smallest-sums/

class Solution(object):
def kSmallestPairs(self, nums1, nums2, k):
"""
:type nums1: List[int]
:type nums2: List[int]
:type k: int
:rtype: List[List[int]]
"""
res = []
len1, len2 = len(nums1), len(nums2)
if not len1 or not len2: return res
heap = []
for x in range(len1):
heapq.heappush(heap, (nums1[x] + nums2[0], x, 0))
while len(res) < min(k, len1 * len2):
sum, i, j = heapq.heappop(heap)
res.append([nums1[i], nums2[j]])
if j + 1 < len2:
heapq.heappush(heap, (nums1[i] + nums2[j + 1], i, j + 1))
return res

日期

2018 年 3 月 14 日 –霍金去世日

【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)的更多相关文章

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

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

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

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

  5. 373. Find K Pairs with Smallest Sums (java,优先队列)

    题目: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Def ...

  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. [LC] 373. 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 查找和最小的K对数字

    给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k.定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2.找到和最小的 k 对数字 (u1,v1 ...

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

随机推荐

  1. Python—python2.7.5升级到2.7.14或者直接升级到3.6.4

    python2.7.5升级到2.7.14 1.安装升级GCC yum install -y gcc* openssl openssl-devel ncurses-devel.x86_64  bzip2 ...

  2. ysoserial-CommonsBeanutils1的shiro无依赖链改造

    ysoserial-CommonsBeanutils1的shiro无依赖链改造 一.CB1利用链分析 此条利用链需要配合Commons-Beanutils组件来进行利用,在shiro中是自带此组件的. ...

  3. 《手把手教你》系列技巧篇(四十八)-java+ selenium自动化测试-判断元素是否可操作(详解教程)

    1.简介 webdriver有三种判断元素状态的方法,分别是isEnabled,isSelected 和 isDisplayed,其中isSelected在前面的内容中已经简单的介绍了,isSelec ...

  4. HDFS【Namenode、SecondaryNamenode、Datanode】

    目录 一. NameNode和SecondaryNameNode 1.NN和2NN 工作机制 2. NN和2NN中的fsimage.edits分析 3.checkpoint设置 4.namenode故 ...

  5. Js数组内对象去重

    let person = [ {id: 0, name: "小明"}, {id: 1, name: "小张"}, {id: 2, name: "小李& ...

  6. 【排序算法】——冒泡排序、选择排序、插入排序、Shell排序等排序原理及Java实现

    排序 1.定义: 所谓排序,即是整理文件中的内容,使其按照关键字递增或递减的顺序进行排列. 输入:n个记录,n1,n2--,其对应1的关键字为k1,k2-- 输出:n(i1),n(i2)--,使得k( ...

  7. oracle中注释都是问号?中文显示不出来问题

    本人在工作中需要把开发上的库恢复到自己的虚拟机里面,然而捣鼓了许久建立好数据库之后,在使用建表语句初始化表的时候,发现注释都是????? 然后一脸懵逼不知何解,网上一大堆是说修改环境变量 NLS_LA ...

  8. zabbix之监控Nginx连接数

    #;下载Nginx (编译的时候必须加上此选项 --with-http_stub_status_module) 官网地址:http://nginx.org/en/docs/http/ngx_http_ ...

  9. springboot+vue脚手架使用nginx前后端分离

    1.vue配置 /** * * 相对于该配置的nginx服务器请参考nginx配置文件 * */ module.exports = { // 基本路径 publicPath: '/', // 输出文件 ...

  10. 【编程思想】【设计模式】【行为模式Behavioral】catalog

    Python版 https://github.com/faif/python-patterns/blob/master/behavioral/catalog.py #!/usr/bin/env pyt ...