当前位置:网站首页>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;
}
边栏推荐
- 软件测试要达到一个什么水平才能找到一份9K的工作?
- 观察者(observer)模式(一)
- Teach you how to configure Jenkins automated email notifications
- 数字图像隐写术之JPEG 隐写分析
- Likou Daily Question - Day 46 - 704. Binary Search
- leetcode-1161: Maximum in-layer element sum
- 程序员转正述职报告/总结
- Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
- 九州云入选“可信云最新评估体系及2022年通过评估企业名单”
- MySQL stored procedure
猜你喜欢

手把手教你配置Jenkins自动化邮件通知

GCC Rust获批将被纳入主线代码库,或将于GCC 13中与大家见面

The difference between 4G communication module CAT1 and CAT4

倍增、DFS序

Jiuzhou Cloud was selected into the "Trusted Cloud's Latest Evaluation System and the List of Enterprises Passing the Evaluation in 2022"

Nacos

Ticmp - 更快的让应用从 MySQL 迁移到 TiDB

斩获BAT、TMD技术专家Offer,我都经历了什么?

1782. Count the number of point pairs Double pointer

设置浏览器滚动条样式
随机推荐
VSCode插件:嵌套注释
Simple confession page
Overview of prometheus monitoring
12张图带你彻底搞懂服务限流、熔断、降级、雪崩
1782. 统计点对的数目 双指针
GCC Rust获批将被纳入主线代码库,或将于GCC 13中与大家见面
Gateway routing configuration
类似 MS Project 的项目管理工具有哪些
The difference between 4G communication module CAT1 and CAT4
MySql的安装配置超详细教程与简单的建库建表方法
TiDB之rawkv升级之路v5.0.4--&gt;v6.1.0
Multiplication, DFS order
case语句的综合结果,你究竟会了吗?【Verilog高级教程】
TiDB 在长银五八消费金融核心系统适配经验分享
仿牛客网项目总结
计算S=a+aa+…+aa…a
蛮力法/邻接矩阵 广度优先 有向带权图 无向带权图
内网渗透——提权
What is the ideal college life?
leetcode-1161: Maximum in-layer element sum