当前位置:网站首页>Between two orderly array of additive and Topk problem
Between two orderly array of additive and Topk problem
2022-07-31 01:46:00 【Xiao Lu wants to brush the force and deduct the question】
前言
描述
给定两个有序数组arr1和arr2,再给定一个整数k,返回来自arr1和arr2的两个数相加和最大的前k个,两个数必须分别来自两个数组
按照降序输出
[要求]
时间复杂度为O(k \log k)O(klogk)
输入描述:
第一行三个整数N, K分别表示数组arr1, arr2的大小,and the number of inquiries required
接下来一行N个整数,表示arr1内的元素
再接下来一行N个整数,表示arr2内的元素
输出描述:
输出K个整数表示答案
解题思路
大根堆
arr1do line correspondence
arr2Do column correspondence
The maximum value is the lower right corner
The left side and the top side are pulled out into the pile
until the collectionK个为止
注意:Prevent the same location from entering the heap
Make sure that you do not enter the large root heap repeatedly
代码
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();
}
// A structure placed into a large root heap
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;
}
}
// Comparators that generate large root heaps
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;
}
边栏推荐
- 蛮力法/邻接矩阵 广度优先 有向带权图 无向带权图
- What are the project management tools like MS Project
- 程序员转正述职报告/总结
- Set the browser scrollbar style
- VSCode Plugin: Nested Comments
- System design. Short chain system design
- 【genius_platform软件平台开发】第七十四讲:window环境下的静态库和动态库的一些使用方法(VC环境)
- tkinter模块高级操作(二)—— 界面切换效果、立体阴影字效果及gif动图的实现
- Multiplication, DFS order
- 一个无经验的大学毕业生,可以转行做软件测试吗?我的真实案例
猜你喜欢
JPEG Steganalysis of Digital Image Steganography
What does a software test report contain?
Chi-square distribution of digital image steganography
leetcode-1161: Maximum in-layer element sum
System design. Short chain system design
coldfusion文件读取漏洞(CVE-2010-2861)
leetcode-1161:最大层内元素和
Installation problem corresponding to tensorflow and GPU version
Nacos
《云原生的本手、妙手和俗手》——2022全国新高考I卷作文
随机推荐
934. 最短的桥
软件测试要达到一个什么水平才能找到一份9K的工作?
CV-Model【3】:MobileNet v2
MySQL stored procedure
Arbitrum 专访 | L2 Summer, 脱颖而出的 Arbitrum 为开发者带来了什么?
【flask入门系列】Flask-SQLAlchemy的使用
MySQL (6)
1.非类型模板参数 2.模板的特化 3.继承讲解
Meta元宇宙部门第二季度亏损28亿 仍要继续押注?元宇宙发展尚未看到出路
1782. Count the number of point pairs Double pointer
蛮力法/邻接矩阵 广度优先 有向带权图 无向带权图
"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
SQLserver查询最近三个月的数据,语句该怎么写sqlserver
【网络安全】文件上传靶场通关(1-11关)
221. 最大正方形
【微信小程序】一文带你了解数据绑定、事件绑定以及事件传参、数据同步
MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
Google官方控件ShapeableImageView使用
《MySQL数据库进阶实战》读后感(SQL 小虚竹)