题目:

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.

分析:求前k小的组合,组合之间的大小依据是数据对和的大小。同样采用优先队列,队首元素为较小值。

代码:

public class Solution {
public List<int[]> kSmallestPairs(int[] nums1, int[] nums2, int k) {
if(nums1 == null || nums2 == null || k <= 0)
return null;
if(nums1.length == 0 || nums2.length == 0)
return new ArrayList<int[]>();
if(nums1.length * nums2.length < k)
k = nums1.length * nums2.length;
//the priorityqueue 优先队列
PriorityQueue<Pair> queue = new PriorityQueue<Pair>();
//result
List<int[]> list = new ArrayList<int[]>();
//首先将nums1同nums2[0]的所有组合入队列
for(int i = 0; i < nums1.length; ++i)
queue.add(new Pair(i,0,nums1[i],nums2[0]));
//获取k个最小值
for(int n = 0; n < k; ++n){
Pair tmp = queue.poll();
int[] ele = tmp.pair; //数值对
list.add(ele); //加入结果集
if(tmp.j == nums2.length - 1)
continue;
queue.add(new Pair(tmp.i,tmp.j + 1,nums1[tmp.i],nums2[tmp.j + 1]));
}
return list;
}
}
//定义pair的数据结构
class Pair implements Comparable<Pair>{
int[] pair = new int[2]; //定义数组对
int i; //第一个元素在nums1中的下标
int j; //第二个元素在nums2中的下标
public Pair(int i,int j,int e1,int e2){
pair[0] = e1;
pair[1] = e2;
this.i = i;
this.j = j;
} @Override
public int compareTo(Pair that){ //升序排列
return (pair[0] + pair[1]) - (that.pair[0] + that.pair[1]);
}
}

373. Find K Pairs with Smallest Sums (java,优先队列)的更多相关文章

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

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

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

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

  7. 373 Find K Pairs with Smallest Sums 查找和最小的K对数字

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

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

  9. Leetcode Find K Pairs with smallest sums

    本题的特点在于两个list nums1和nums2都是已经排序好的.本题如果把所有的(i, j)组合都排序出来,再取其中最小的K个.其实靠后的很多组合根本用不到,所以效率较低,会导致算法超时.为了简便 ...

随机推荐

  1. 使用kubeadm 安装 kubernetes 1.12.0

    目录 简介: 架构说明: 系统配置: 1.1 关闭防火墙 1.2 禁用SELinux 1.3 关闭系统Swap 1.4 安装docker 使用kubeadm部署Kubernetes: 2.1 安装ku ...

  2. P2120 [ZJOI2007]仓库建设(dp+斜率优化)

    思路 首先暴力DP显然,可以得20分 加上一个前缀和优化,可以得到40分 然后上斜率优化 设\(sum_i\)为\(\sum_{1}^iP_i\),\(sump_i\)为\(\sum_{1}^{i}P ...

  3. Kylin web界面 知识点介绍

    Big Data Era: 1.More and more data becoming available on Hadoop2.Limitations in existing Business ...

  4. ExceptionLogger

    应用1:webconfig.cs中设置 public static class WebApiConfig { public static void Register(HttpConfiguration ...

  5. PTA 7-1 整数分解为若干项之和(20 分)

    7-1 整数分解为若干项之和(20 分) 将一个正整数N分解成几个正整数相加,可以有多种分解方法,例如7=6+1,7=5+2,7=5+1+1,….编程求出正整数N的所有整数分解式子. 输入格式: 每个 ...

  6. 最小 base64 图片

    http://www.webhek.com/post/base64-encode-of-1x1px-transparent-gif.html Base64 Encode 1x1px透明GIF图片 Ba ...

  7. Oracle Initialzation error instantclient_11_2 32位客户端问题

    本地安装完oracle 用PLSql 连接是一堆的问题,折腾一上午终于解决了 下载一个 instantclient_11_2 32位的oracle客户端.放在oracle安装目录product下,在把 ...

  8. 蚂蚁金服“定损宝”现身AI顶级会议NeurIPS

    小蚂蚁说: 长期以来,车险定损(通过现场拍摄定损照片确定车辆损失,以作为保险公司理赔的依据)是车险理赔中最为重要的操作环节.以往传统保险公司的车险处理流程,一般为报案.现场查勘.提交理赔材料.审核.最 ...

  9. Easy Graphics Engine vs2015使用

    vs2017: 下载 https://pan.baidu.com/s/1qWxAgeK 里面的 “ege19.01_vs2017 (推荐, 修正win10 1809 上无法正常运行的问题).zip”  ...

  10. python 比较两个yaml文件

    import yaml with open("a.yaml") as f: with open("a.yaml") as k: ): x=f.readline( ...