当前位置:网站首页>两个有序数组间相加和的Topk问题
两个有序数组间相加和的Topk问题
2022-07-31 01:38:00 【小卢要刷力扣题】
前言
描述
给定两个有序数组arr1和arr2,再给定一个整数k,返回来自arr1和arr2的两个数相加和最大的前k个,两个数必须分别来自两个数组
按照降序输出
[要求]
时间复杂度为O(k \log k)O(klogk)
输入描述:
第一行三个整数N, K分别表示数组arr1, arr2的大小,以及需要询问的数
接下来一行N个整数,表示arr1内的元素
再接下来一行N个整数,表示arr2内的元素
输出描述:
输出K个整数表示答案
解题思路
大根堆
arr1做行对应
arr2做列对应
最大值是右下角
左边跟上边拽出来进堆
一直到收集K个为止
注意:防止同一个位置进入堆
要保证进大根堆不要重复进
代码
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int K = sc.nextInt();
int[] arr1 = new int[N];
int[] arr2 = new int[N];
for (int i = 0; i < N; i++) {
arr1[i] = sc.nextInt();
}
for (int i = 0; i < N; i++) {
arr2[i] = sc.nextInt();
}
int[] topK = topKSum(arr1, arr2, K);
for (int i = 0; i < K; i++) {
System.out.print(topK[i] + " ");
}
System.out.println();
sc.close();
}
// 放入大根堆中的结构
public static class Node {
public int index1;// arr1中的位置
public int index2;// arr2中的位置
public int sum;// arr1[index1] + arr2[index2]的值
public Node(int i1, int i2, int s) {
index1 = i1;
index2 = i2;
sum = s;
}
}
// 生成大根堆的比较器
public static class MaxHeapComp implements Comparator<Node> {
@Override
public int compare(Node o1, Node o2) {
return o2.sum - o1.sum;
}
}
public static int[] topKSum(int[] arr1, int[] arr2, int topK) {
if (arr1 == null || arr2 == null || topK < 1) {
return null;
}
int N = arr1.length;
int M = arr2.length;
topK = Math.min(topK, N * M);
int[] res = new int[topK];
int resIndex = 0;
PriorityQueue<Node> maxHeap = new PriorityQueue<>(new MaxHeapComp());
HashSet<Long> set = new HashSet<>();
int i1 = N - 1;
int i2 = M - 1;
maxHeap.add(new Node(i1, i2, arr1[i1] + arr2[i2]));
set.add(x(i1, i2, M));
while (resIndex != topK) {
Node curNode = maxHeap.poll();
res[resIndex++] = curNode.sum;
i1 = curNode.index1;
i2 = curNode.index2;
set.remove(x(i1, i2, M));
if (i1 - 1 >= 0 && !set.contains(x(i1 - 1, i2, M))) {
set.add(x(i1 - 1, i2, M));
maxHeap.add(new Node(i1 - 1, i2, arr1[i1 - 1] + arr2[i2]));
}
if (i2 - 1 >= 0 && !set.contains(x(i1, i2 - 1, M))) {
set.add(x(i1, i2 - 1, M));
maxHeap.add(new Node(i1, i2 - 1, arr1[i1] + arr2[i2 - 1]));
}
}
return res;
}
public static long x(int i1, int i2, int M) {
return (long) i1 * (long) M + (long) i2;
}
边栏推荐
- coldfusion文件读取漏洞(CVE-2010-2861)
- Analyze the capabilities and scenarios of the cloud native message flow system Apache Pulsar
- ShardingSphere's vertical sub-database sub-table actual combat (5)
- ROS Action通信
- I have been working in software testing for 3 years, how did I go from just getting started to automated testing?
- 软件测试基础接口测试-入门Jmeter,你要注意这些事
- 【genius_platform软件平台开发】第七十四讲:window环境下的静态库和动态库的一些使用方法(VC环境)
- 射频器件的基本参数1
- MySQL stored procedure
- kotlin中函数作为参数和函数作为返回值实例练习
猜你喜欢
Centos 7.9 install PostgreSQL14.4 steps
C语言_结构体指针数组函数选票系统
The sword refers to offer17---print the n digits from 1 to the largest
Xiaohei's leetcode journey: 104. The maximum depth of a binary tree
太阳能板最大面积 od js
Word/Excel 固定表格大小,填写内容时,表格不随单元格内容变化
ROS Action communication
35. 反转链表
What have I experienced when I won the offer of BAT and TMD technical experts?
【genius_platform软件平台开发】第七十四讲:window环境下的静态库和动态库的一些使用方法(VC环境)
随机推荐
验证 XML 文档
TiDB 在长银五八消费金融核心系统适配经验分享
leetcode-952:按公因数计算最大组件大小
【flask入门系列】Flask-SQLAlchemy的使用
MySQL installation tutorial (detailed, package teaching package~)
力扣每日一题-第46天-704. 二分查找
What have I experienced when I won the offer of BAT and TMD technical experts?
想要写出好的测试用例,先要学会测试设计
"Actual Combat" based on part-of-speech extraction in the field of e-commerce and its decision tree model modeling
Basic Parameters of RF Devices 1
tkinter模块高级操作(二)—— 界面切换效果、立体阴影字效果及gif动图的实现
SQLserver查询最近三个月的数据,语句该怎么写sqlserver
设置浏览器滚动条样式
充电效果模拟
Set the browser scrollbar style
程序员转正述职报告/总结
Shell变量与赋值、变量运算、特殊变量
rpm安装postgresql12
聚簇索引和非聚簇索引到底有什么区别
24. Please talk about the advantages and disadvantages of the singleton pattern, precautions, usage scenarios