【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. Docker镜像相关操作

    批量导入镜像 ll *.tgz|awk '{print $NF}'|sed -r 's#(.*)#docker load -i \1#' |bash 批量打tag docker images | se ...

  2. C#表头固定

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="info.ascx.cs& ...

  3. 日常Java(测试 (二柱)修改版)2021/9/22

    题目: 一家软件公司程序员二柱的小孩上了小学二年级,老师让家长每天出30道四则运算题目给小学生做. 二柱一下打印出好多份不同的题目,让孩子做了.老师看了作业之后,对二柱赞许有加.别的老师闻讯, 问二柱 ...

  4. Flink(二)【架构原理,组件,提交流程】

    目录 一.运行架构 1.架构 2.组件 二.核心概念 TaskManager . Slots Parallelism(并行度) Task .Subtask Operator Chains(任务链) E ...

  5. 数仓day04----日志预处理2

    1.详细描述idmap的整个计算方案 (1)使用SparkSession对象读取用户不同类别的埋点日志,解析并抽取出相应的标识id,使用union进行合并,得到装有汇总标识id的rdd(ids) (2 ...

  6. STL学习笔记1

    STL六大部件 容器.分配器.算法.迭代器.适配器.仿函数 他们的关系如下

  7. Linux的小知识

    1. top 命令可以在Linux下查看任务管理器和当前进程使用资源情况. 2. Ctrl+c 即可退出,然后使用 kill+进程号 命令可杀死指定进程 3.在Linux的 /etc/rc.local ...

  8. Oracle异常处理——ORA-01502:索引或这类索引的分区处于不可用状态

    Oracle异常处理--ORA-01502:索引或这类索引的分区处于不可用状态参考自:https://www.cnblogs.com/lijiaman/p/9277149.html 1.原因分析经过查 ...

  9. 【Linux】【Basis】块存储,文件存储,对象存储

    1. 块存储: 定义:这种接口通常以QEMU Driver或者Kernel Module的方式存在,这种接口需要实现Linux的Block Device的接口或者QEMU提供的Block Driver ...

  10. 【Python】【Module】hashlib

    用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 import hashlib # ######## ...