当前位置:网站首页>两个有序数组间相加和的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;
}
边栏推荐
- 程序员转正述职报告/总结
- MySQL的安装教程(嗷嗷详细,包教包会~)
- MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
- "Actual Combat" based on part-of-speech extraction in the field of e-commerce and its decision tree model modeling
- 87. Convert String to Integer
- Mysql: Invalid default value for TIMESTAMP
- 聚簇索引和非聚簇索引到底有什么区别
- 小黑leetcode之旅:104. 二叉树的最大深度
- Ticmp - 更快的让应用从 MySQL 迁移到 TiDB
- MySQL (6)
猜你喜欢

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

【genius_platform软件平台开发】第七十四讲:window环境下的静态库和动态库的一些使用方法(VC环境)

Xiaohei's leetcode journey: 104. The maximum depth of a binary tree

Interprocess communication study notes

调度中心xxl-Job

黄东旭:TiDB的优势是什么?

Shell变量与赋值、变量运算、特殊变量

小黑leetcode之旅:117. 填充每个节点的下一个右侧节点指针 II

【Map与Set】之LeetCode&牛客练习

使用docker安装mysql
随机推荐
MySQL installation tutorial (detailed, package teaching package~)
Word 表格跨页,仍然显示标题
手把手教你配置Jenkins自动化邮件通知
MySQL的安装教程(嗷嗷详细,包教包会~)
Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
基于Keras_bert模型的Bert使用与字词预测
数字图像隐写术之JPEG 隐写分析
Centos 7.9 install PostgreSQL14.4 steps
斩获BAT、TMD技术专家Offer,我都经历了什么?
太阳能板最大面积 od js
link与@import的区别
ECCV 2022 华科&ETH提出首个用于伪装实例分割的一阶段Transformer的框架OSFormer!代码已开源!
Basic Parameters of RF Devices 1
MySQL (6)
VSCode插件:嵌套注释
倍增、DFS序
rpm install postgresql12
勾股数元组 od js
ShardingSphere's vertical sub-database sub-table actual combat (5)
TiDB 操作实践 -- 备份与恢复