当前位置:网站首页>在两个有序数组中找到整体第K小的数可以做到O(log(Min(M,N)))
在两个有序数组中找到整体第K小的数可以做到O(log(Min(M,N)))
2022-06-24 21:19:00 【N. LAWLIET】
问题:
在两个有序数组中找到整体第K小的数可以做到O(log(Min(M,N)))
解题思路
解:分情况讨论
1)1<=k<=短数组长度
2)长数组长度<=k<=整体数组长度
3)短数组长度<=k<=长数组长度
代码
public class Code03_FindKthMinNumber {
// 进阶问题 : 在两个都有序的数组中,找整体第K小的数
// 可以做到O(log(Min(M,N)))
public static int findKthNum(int[]arr1,int[] arr2,int kth) {
int[] longs = arr1.length>arr2.length?arr1:arr2;
int[] shorts = arr1.length<arr2.length?arr1:arr2;
int s = shorts.length;
int l = longs.length;
if(kth<=s) {
return getUpMedian(shorts, 0, kth-1, longs, 0, kth-1);
}
if(kth>=l) {
if(shorts[kth-l-1]>longs[l-1]) {
return shorts[kth-l-1];
}
if(longs[kth-s-1]>shorts[s-1]) {
return longs[kth-s-1];
}
return getUpMedian(shorts, kth-l, s-1, longs, kth-s, l-1);
}
//s<kth<l
else {
if(longs[kth-s-1]>shorts[s-1]) {
return longs[kth-s-1];
}
return getUpMedian(shorts, 0, s-1, longs,kth-s, l-1);
}
}
//比较两个等长数组中位数
public static int getUpMedian(int[]arr1,int s1,int e1,int[]arr2,int s2,int e2) {
int mid1 = 0;
int mid2 = 0;
while(s1<e1) {
mid1 = (s1+e1)/2;
mid2 = (s2+e2)/2;
if(arr1[mid1] == arr2[mid2]) {
return arr1[mid1];
}
if(((e1-s1+1)&1) == 1) {
//单数情况
if(arr1[mid1]>arr2[mid2]) {
if(arr2[mid2]>arr1[mid1-1]) {
return arr2[mid2];
}
e1 = mid1-1;
s2 = mid2 +1;
}else {
if(arr1[mid1]>=arr2[mid2-1]) {
return arr1[mid1];
}
e2 = mid2 - 1;
s1 = mid1 +1;
}
}else {
if(arr1[mid1]>arr2[mid2]) {
e1 = mid1;
s2 = mid2+1;
}else {
e2 = mid2;
s1 = mid1 + 1;
}
}
}
return Math.min(arr1[mid1], arr2[mid2]);
}
}
边栏推荐
- 4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
- Texture enhancement
- 汇编语言(4)函数传参
- void* 指针
- Convolution and transpose convolution
- Tencent has completed the comprehensive cloud launch to build the largest cloud native practice in China
- Bi skill - judge 0 and null
- 木瓜蛋白酶中英文说明书
- 弹性蛋白酶中英文说明书
- 云开发技术峰会·公益编程挑战赛【火热报名中】!
猜你喜欢

Bi-sql index

天书夜读笔记——内存分页机制

leetcode:2104. 子数组范围和

4 ans d'expérience de travail, 5 modes de communication Multi - thread ne peuvent pas être décrits, vous osez croire?

Q1季度逆势增长的华为笔电,正引领PC进入“智慧办公”时代

4年工作經驗,多線程間的5種通信方式都說不出來,你敢信?

百度语音合成语音文件并在网站中展示

Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source! (high performance + high customization)

4年工作经验,多线程间的5种通信方式都说不出来,你敢信?

Bi-sql top
随机推荐
【直播回顾】2022腾讯云未来社区城市运营方招募会暨SaaS 2.0新品发布会!
Boutique enterprise class powerbi application pipeline deployment
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
汇编语言(2)基础知识-debug
Yasea APK Download Image
[live review] 2022 Tencent cloud future community city operator recruitment conference and SaaS 2.0 new product launch!
广发期货安全吗?开户需要什么东西?
Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop
Ecological escort cloud service providers wave "Intel flag"
期望与方差
Welcome to the new world of Lenovo smart screen
JVM指令
C语言边界计算和不对称边界
腾讯云WeCity丨你好 2022!
[untitled]
天书夜读笔记——8.4 diskperf反汇编
C language boundary calculation and asymmetric boundary
Bi-sql delete
【实用系列】家内wifi全覆盖
Tencent cloud wecity Industry joint collaborative innovation to celebrate the New Year!