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.
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,优先队列)的更多相关文章
- 【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 找和最小的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 ...
- #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 ...
- [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 ...
- Leetcode Find K Pairs with smallest sums
本题的特点在于两个list nums1和nums2都是已经排序好的.本题如果把所有的(i, j)组合都排序出来,再取其中最小的K个.其实靠后的很多组合根本用不到,所以效率较低,会导致算法超时.为了简便 ...
随机推荐
- Winform异步解决窗体耗时操作(Action专门用于无返回值,Func专门用于有返回值)
http://blog.csdn.net/config_man/article/details/25578767 #region 调用timer控件实时查询开关机时间 private void tim ...
- [HDU 1976] Software Version
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1976 #include<iostream> #include<cstdio> ...
- Apple ID双重认证验证码无法输入问题
问题:用Apple ID登录老版本ios系统时,会提示“”需要提供Apple ID验证码才能登陆.请键入您的密码,并随后键入显示在您其他设备上的验证码”. 解决:老版本系统无输入验证码的地方,那就把验 ...
- Derek解读Bytom源码-protobuf生成比原核心代码
作者:Derek 简介 Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom ...
- Spring项目JUnit测试报错ClassNotFoundException解决
Eclipse项目上有红色感叹号,各包显示正常.用JUnit测试部分能运行,部分报错,报错如下: Class not found UserTestjava.lang.ClassNotFoundExce ...
- C#Listview添加数据,选中最后一行,滚屏
this.listView.Items.Add(lvi); this.listView.EnsureVisible(this.listView.Items.Count - 1); this.listV ...
- WebSocket 教程
转载自:http://www.ruanyifeng.com/blog/2017/05/websocket.html WebSocket 是一种网络通信协议,很多高级功能都需要它. 本文介绍 WebSo ...
- [JS]计算字符串中出现最多的字符和其出现次数
这是一道面试题 此处是利用Obj来解决的,当然不只此一种方法. //思路:遍历数组,拿到一个字符,并将之以 "字符":出现次数 的key:value形式存到对象中. //如果此字符 ...
- 简单理解Hadoop架构原理
一.前奏 Hadoop是目前大数据领域最主流的一套技术体系,包含了多种技术. 包括HDFS(分布式文件系统),YARN(分布式资源调度系统),MapReduce(分布式计算系统),等等. 有些朋友可能 ...
- 学习笔记26— roc曲线(python)
一.概念: 准确率(Accuracy), 精确率(Precision), 召回率(Recall)和F1-Measure 机器学习(ML), 自然语言处理(NLP), 信息检索(IR)等领域, 评估(E ...