两个有序数组间相加和的Top k问题

2024-06-21 21:48

本文主要是介绍两个有序数组间相加和的Top k问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这里写图片描述

import java.util.*;
//两个有序数组间相加和的Top k问题
public class FindArrTopK{//堆节点的定义public static class HeapNode{public int row;public int col;public int value;public HeapNode(int row,int col,int value){this.row=row;this.col=col;this.value=value;}}//获得top k问题public static int[] topKSum(int[] a1, int[] a2, int topK) {if (a1 == null || a2 == null || topK < 1) {return null;}topK = Math.min(topK, a1.length * a2.length);HeapNode[] heap = new HeapNode[topK + 1];int heapSize = 0;int headR = a1.length - 1;int headC = a2.length - 1;int uR = -1;int uC = -1;int lR = -1;int lC = -1;heapInsert(heap, heapSize++, headR, headC, a1[headR] + a2[headC]);HashSet<String> positionSet = new HashSet<String>();int[] res = new int[topK];int resIndex = 0;while (resIndex != topK) {HeapNode head = popHead(heap, heapSize--);res[resIndex++] = head.value;headR = head.row;headC = head.col;uR = headR - 1;uC = headC;if (headR != 0 && !isContains(uR, uC, positionSet)) {heapInsert(heap, heapSize++, uR, uC, a1[uR] + a2[uC]);addPositionToSet(uR, uC, positionSet);}lR = headR;lC = headC - 1;if (headC != 0 && !isContains(lR, lC, positionSet)) {heapInsert(heap, heapSize++, lR, lC, a1[lR] + a2[lC]);addPositionToSet(lR, lC, positionSet);}}return res;}public static HeapNode popHead(HeapNode[] heap, int heapSize) {HeapNode res = heap[0];swap(heap, 0, heapSize - 1);heap[--heapSize] = null;heapify(heap, 0, heapSize);return res;}public static void heapify(HeapNode[] heap, int index, int heapSize) {int left = index * 2 + 1;int right = index * 2 + 2;int largest = index;while (left < heapSize) {if (heap[left].value > heap[index].value) {largest = left;}if (right < heapSize && heap[right].value > heap[largest].value) {largest = right;}if (largest != index) {swap(heap, largest, index);} else {break;}index = largest;left = index * 2 + 1;right = index * 2 + 2;}}public static void heapInsert(HeapNode[] heap, int index, int row, int col,int value) {heap[index] = new HeapNode(row, col, value);int parent = (index - 1) / 2;while (index != 0) {if (heap[index].value > heap[parent].value) {swap(heap, parent, index);index = parent;parent = (index - 1) / 2;} else {break;}}}public static void swap(HeapNode[] heap, int index1, int index2) {HeapNode tmp = heap[index1];heap[index1] = heap[index2];heap[index2] = tmp;}public static boolean isContains(int row, int col, HashSet<String> set) {return set.contains(String.valueOf(row + "_" + col));}public static void addPositionToSet(int row, int col, HashSet<String> set) {set.add(String.valueOf(row + "_" + col));}// For test, this method is inefficient but absolutely rightpublic static int[] topKSumTest(int[] arr1, int[] arr2, int topK) {int[] all = new int[arr1.length * arr2.length];int index = 0;for (int i = 0; i != arr1.length; i++) {for (int j = 0; j != arr2.length; j++) {all[index++] = arr1[i] + arr2[j];}}Arrays.sort(all);int[] res = new int[Math.min(topK, all.length)];index = all.length - 1;for (int i = 0; i != res.length; i++) {res[i] = all[index--];}return res;}public static int[] generateRandomSortArray(int len) {int[] res = new int[len];for (int i = 0; i != res.length; i++) {res[i] = (int) (Math.random() * 50000) + 1;}Arrays.sort(res);return res;}public static boolean isEqual(int[] arr1, int[] arr2) {if (arr1 == null || arr2 == null || arr1.length != arr2.length) {return false;}for (int i = 0; i != arr1.length; i++) {if (arr1[i] != arr2[i]) {return false;}}return true;}//打印数组public static void printArray(int[] arr) {for (int i = 0; i != arr.length; i++) {System.out.print(arr[i] + " ");}System.out.println();}public static void main(String[]args){//System.out.println("Hello");int[]arr1={1,2,3,4,5};int[]arr2={3,5,7,9,11};int k=4;int[]res = topKSum(arr1,arr2,k);printArray(res);}
}

这里写图片描述

这篇关于两个有序数组间相加和的Top k问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1082424

相关文章

linux生产者,消费者问题

pthread_cond_wait() :用于阻塞当前线程,等待别的线程使用pthread_cond_signal()或pthread_cond_broadcast来唤醒它。 pthread_cond_wait() 必须与pthread_mutex 配套使用。pthread_cond_wait()函数一进入wait状态就会自动release mutex。当其他线程通过pthread

问题:第一次世界大战的起止时间是 #其他#学习方法#微信

问题:第一次世界大战的起止时间是 A.1913 ~1918 年 B.1913 ~1918 年 C.1914 ~1918 年 D.1914 ~1919 年 参考答案如图所示

2024.6.24 IDEA中文乱码问题(服务器 控制台 TOMcat)实测已解决

1.问题产生原因: 1.文件编码不一致:如果文件的编码方式与IDEA设置的编码方式不一致,就会产生乱码。确保文件和IDEA使用相同的编码,通常是UTF-8。2.IDEA设置问题:检查IDEA的全局编码设置和项目编码设置是否正确。3.终端或控制台编码问题:如果你在终端或控制台看到乱码,可能是终端的编码设置问题。确保终端使用的是支持你的文件的编码方式。 2.解决方案: 1.File -> S

vcpkg安装opencv中的特殊问题记录(无法找到opencv_corexd.dll)

我是按照网上的vcpkg安装opencv方法进行的(比如这篇:从0开始在visual studio上安装opencv(超详细,针对小白)),但是中间出现了一些别人没有遇到的问题,虽然原因没有找到,但是本人给出一些暂时的解决办法: 问题1: 我在安装库命令行使用的是 .\vcpkg.exe install opencv 我的电脑是x64,vcpkg在这条命令后默认下载的也是opencv2:x6

问题-windows-VPN不正确关闭导致网页打不开

为什么会发生这类事情呢? 主要原因是关机之前vpn没有关掉导致的。 至于为什么没关掉vpn会导致网页打不开,我猜测是因为vpn建立的链接没被更改。 正确关掉vpn的时候,会把ip链接断掉,如果你不正确关掉,ip链接没有断掉,此时你vpn又是没启动的,没有域名解析,所以就打不开网站。 你可以在打不开网页的时候,把vpn打开,你会发现网络又可以登录了。 方法一 注意:方法一虽然方便,但是可能会有

vue同页面多路由懒加载-及可能存在问题的解决方式

先上图,再解释 图一是多路由页面,图二是路由文件。从图一可以看出每个router-view对应的name都不一样。从图二可以看出层路由对应的组件加载方式要跟图一中的name相对应,并且图二的路由层在跟图一对应的页面中要加上components层,多一个s结尾,里面的的方法名就是图一路由的name值,里面还可以照样用懒加载的方式。 页面上其他的路由在路由文件中也跟图二是一样的写法。 附送可能存在

vue+elementui--$message提示框被dialog遮罩层挡住问题解决

最近碰到一个先执行this.$message提示内容,然后接着弹出dialog带遮罩层弹框。那么问题来了,message提示框会默认被dialog遮罩层挡住,现在就是要解决这个问题。 由于都是弹框,问题肯定是出在z-index比重问题。由于用$message方式是写在js中而不是写在html中所以不是很好直接去改样式。 不过好在message组件中提供了customClass 属性,我们可以利用

Visual Studio中,MSBUild版本问题

假如项目规定了MSBUild版本,那么在安装完Visual Studio后,假如带的MSBUild版本与项目要求的版本不符合要求,那么可以把需要的MSBUild添加到系统中,然后即可使用。步骤如下:            假如项目需要使用V12的MSBUild,而安装的Visual Studio带的MSBUild版本为V14。 ①到MSDN下载V12 MSBUild包,把V12包解压到目录(

YOLO v3 训练速度慢的问题

一天一夜出了两个模型,仅仅迭代了200次   原因:编译之前没有将Makefile 文件里的GPU设置为1,编译的是CPU版本,必须训练慢   解决方案: make clean  vim Makefile make   再次训练 速度快了,5分钟迭代了500次

Pycharm配置conda环境(解决新版本无法识别可执行文件问题)

引言: 很多小伙伴在下载最新版本的pycharm或者更新到最新版本后为项目配置conda环境的时候,发现文件夹目录中无法显示可执行文件(一般为python.exe),以下就是本人遇到该问题后试验和解决该问题的一些方法和思路。 一般遇到该问题的人群有两种,一种是刚入门对pycharm进行conda环境配置的小白(例如我),不熟悉相关环境配置的操作和过程,还有一种是入坑pycharm有段时间的老手