当前位置:网站首页>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;
}
边栏推荐
- Gateway routing configuration
- MySql installation and configuration super detailed tutorial and simple method of building database and table
- Crawler text data cleaning
- 设置浏览器滚动条样式
- 一个无经验的大学毕业生,可以转行做软件测试吗?我的真实案例
- Set the browser scrollbar style
- 934. The Shortest Bridge
- [WeChat applet] This article takes you to understand data binding, event binding, event parameter transfer, and data synchronization
- Bert usage and word prediction based on Keras_bert model
- 16、注册中心-consul
猜你喜欢

软件测试基础接口测试-入门Jmeter,你要注意这些事

Xiaohei's leetcode journey: 117. Fill the next right node pointer of each node II

12张图带你彻底搞懂服务限流、熔断、降级、雪崩

【flask入门系列】Flask-SQLAlchemy的使用

最高月薪20K?平均薪资近万...在华为子公司工作是什么体验?

VSCode插件:嵌套注释

leetcode-399:除法求值

软件测试报告有哪些内容?

Maximum monthly salary of 20K?The average salary is nearly 10,000... What is the experience of working in a Huawei subsidiary?

System design. Short chain system design
随机推荐
The difference between 4G communication module CAT1 and CAT4
My first understanding of MySql, and the basic syntax of DDL and DML and DQL in sql statements
聚簇索引和非聚簇索引到底有什么区别
MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
The PC side determines the type of browser currently in use
《云原生的本手、妙手和俗手》——2022全国新高考I卷作文
MySql的安装配置超详细教程与简单的建库建表方法
What have I experienced when I won the offer of BAT and TMD technical experts?
MySQL的分页你还在使劲的limit?
JS逆向之浏览器补环境(一)
Arbitrum Interview | L2 Summer, what does the standout Arbitrum bring to developers?
程序员转正述职报告/总结
leetcode-1161:最大层内元素和
力扣每日一题-第46天-704. 二分查找
Know what DTU is 4GDTU equipment
TiDB 操作实践 -- 备份与恢复
Maximum monthly salary of 20K?The average salary is nearly 10,000... What is the experience of working in a Huawei subsidiary?
Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
关于Redis相关内容的基础学习
PDF 拆分/合并