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个.其实靠后的很多组合根本用不到,所以效率较低,会导致算法超时.为了简便 ...
随机推荐
- 【Finchley】【新特性】Spring Cloud Finchley 新特性
Finchley 正式版的发布貌似经历了相当长的时间,这次的重大发布主要带来了以下 4 项重大更新. 重大更新 1.新增 Spring Cloud Gateway 组件 Spring Cloud Ga ...
- nowcoder 合并回文子串
链接:https://www.nowcoder.com/acm/contest/6/C来源:牛客网题目输入两个字符串A和B,合并成一个串C,属于A和B的字符在C中顺序保持不变.如"abc&q ...
- HDU 4576 Robot
思路 概率dp 既然是求概率,顺推 显然有转移\(dp[i][j]=dp[i-1][j-w]/2+dp[i-1][w]/2\) 然后是环,注意特判一下 环上不要用取模处理循环的情况,会被卡常 代码 # ...
- [easyui] - 在easyui的table中展示提示框
因为在easyui的table中字段过多,而无法展示全时,被迫只能使用这个方法. 使用方式: 在 $('#dg').datagrid({ 后的 queryParams: form2Json('sear ...
- 高级定时器TIM1&TIM8
高级定时器 初识stm32高级定时器: (1)高级控制定时器(TIM1 和 TIM8)和通用定时器在基本 ...
- Oracle 基础学习笔记
知识点 一.登陆数据库: 登陆数据库: sqlplus system/oracle123456 二.新建用户.授权(连接数据库.创建表.表空间.查询某用户下的表) 语法: create user [用 ...
- 【Entity framework】Code First Approach
开篇之前感谢 china_fucan的文章给我的帮助,下面的评论也解决了很多问题同样给予感谢. code first 项目中的ORM框架如果采用的是EF,那么可能会采用code first的方式去使用 ...
- RabbitMq的整理 exchange、route、queue关系
https://blog.csdn.net/samxx8/article/details/47417133
- 【OJ】字符串去重并并按原顺序打印出重复字符
ACM上一道简单的字符串题,从网上找了下类似的代码进行参考外加之个人思考,想到此好思路. 题目大意 任意输入一行字符串,检索重复出现的字符.将原字符串中的重复字符删除后按照原顺序输出,同时按照原顺序输 ...
- 给 layui upload 带每个文件的进度条, .net 后台代码
1.upload.js 扩展 功能利用ajax的xhr属性实现该功能修改过modules中的upload.js文件功能具体实现:在js文件中添加监听函数 //创建监听函数 var xhrOnProgr ...