当前位置:网站首页>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的工作?
GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13
华为od 转骰子 js
prometheus 监控概述
The difference between 4G communication module CAT1 and CAT4
Kyushu cloud as cloud computing standardization excellent member unit
进程间通信学习笔记
case语句的综合结果,你究竟会了吗?【Verilog高级教程】
设置浏览器滚动条样式
Ticmp - 更快的让应用从 MySQL 迁移到 TiDB
随机推荐
System design. Short chain system design
Mysql: Invalid default value for TIMESTAMP
Nacos
GCC Rust获批将被纳入主线代码库,或将于GCC 13中与大家见面
Programmer's debriefing report/summary
kotlin中函数作为参数和函数作为返回值实例练习
leetcode-952:按公因数计算最大组件大小
Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
Word/Excel 固定表格大小,填写内容时,表格不随单元格内容变化
Distributed. Idempotency
CV-Model【3】:MobileNet v2
汉诺塔问题
VSCode Plugin: Nested Comments
coldfusion文件读取漏洞(CVE-2010-2861)
keep-alive缓存组件
Parameter introduction and selection points of wireless module
After reading "MySQL Database Advanced Practice" (SQL Xiao Xuzhu)
ROS Action communication
keep-alive cache component
TiKV主要内存结构和OOM排查总结