【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)
【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)的更多相关文章
- #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 ...
- 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 ...
- 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 ...
- 【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 ...
- [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 ...
- 373 Find K Pairs with Smallest Sums 查找和最小的K对数字
给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k.定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2.找到和最小的 k 对数字 (u1,v1 ...
- [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 ...
随机推荐
- 联盛德 HLK-W806 (六): I2C驱动SSD1306 128x64 OLED液晶屏
目录 联盛德 HLK-W806 (一): Ubuntu20.04下的开发环境配置, 编译和烧录说明 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明 联盛德 HLK-W ...
- c#表中信息点击跳转
OnRowCommand="gridInfoData_RowCommand" <Columns> <asp:ButtonField HeaderText=&quo ...
- java输入代码
import java.util.Scanner; public class Demo59 { public static void main(String[] args) { / ...
- LeetCode两数之和
LeetCode 两数之和 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是 ...
- 对于vue项目更新迭代导致上传至服务器后出现Loading chunk {n} failed和Unexpected token <的解决方式
相信大家对于vue项目的维护与更新中会遇见很多问题,其中有两种情况最为常见. 一种是Loading chunk {n} failed,这种情况出现的原因是vue页面更新上传至服务器后,由于vue默认打 ...
- MapReduce的类型与格式
MapReduce的类型 默认的MR作业 默认的mapper是Mapper类,它将输入的键和值原封不动地写到输出中 默认的partitioner是HashPartitioner,它对每条记录的键进行哈 ...
- canal从mysql拉取数据,并以protobuf的格式往kafka中写数据
大致思路: canal去mysql拉取数据,放在canal所在的节点上,并且自身对外提供一个tcp服务,我们只要写一个连接该服务的客户端,去拉取数据并且指定往kafka写数据的格式就能达到以proto ...
- c#中实现串口通信的几种方法
c#中实现串口通信的几种方法 通常,在C#中实现串口通信,我们有四种方法: 第一:通过MSCOMM控件这是最简单的,最方便的方法.可功能上很难做到控制自如,同时这个控件并不是系统本身所带,所以还得注册 ...
- Linux运维实战之磁盘分区、格式化及挂载(一)
在网络系统中,磁盘和文件系统管理是两个非常基本.同时也是非常重要的管理任务,特别是文件系统管理,因为它与用户权限和整个网络系统的安全息息相关.本次博文的主题是关于Linux系统中磁盘分区.格式化及挂载 ...
- 【Services】【Web】【tomcat】配置tomcat支持https传输
1. 基础: 1.1. 描述:内网的tomcat接到外网nginx转发过来的请求之后需要和外网的客户端进行通讯,为了保证通讯内容的安装,使用tomcat使用https协议. 1.2. 链接:http: ...