当前位置:网站首页>在两个有序数组中找到整体第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]);
}
}
边栏推荐
- 胰蛋白酶中英文说明书
- PHP 利用getid3 获取mp3、mp4、wav等媒体文件时长等数据
- Zuckerberg demonstrated four VR head display prototypes, and meta revealed the "family" of metauniverse
- AUTOCAD——两种延伸方式
- 高考之后,必然会出现以下四种情况:
- How to store dataframe data in pandas into MySQL
- 【直播回顾】2022腾讯云未来社区城市运营方招募会暨SaaS 2.0新品发布会!
- 百度语音合成语音文件并在网站中展示
- 汇编语言(4)函数传参
- 天书夜读笔记——反汇编引擎xde32
猜你喜欢

Abnova丨5-甲基胞嘧啶多克隆抗体中英文说明

Bi-sql - join

Abnova丨A4GNT多克隆抗体中英文说明

实验5 8254定时/计数器应用实验【微机原理】【实验】

“一个优秀程序员可抵五个普通程序员!”

Programmer: did you spend all your savings to buy a house in Shenzhen? Or return to Changsha to live a "surplus" life?

汇编语言(4)函数传参

Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop

Boutique enterprise class powerbi application pipeline deployment

Bi-sql index
随机推荐
云开发技术峰会·公益编程挑战赛【火热报名中】!
The optimism about technology is making Dell achieve more than expected
对技术的乐观,正让戴尔取得比想象中更多的成就
AutoCAD - two extension modes
lnmp环境安装ffmpeg,并在Yii2中使用
Programmer: did you spend all your savings to buy a house in Shenzhen? Or return to Changsha to live a "surplus" life?
Install mysql5.6 under linux64bit - the root password cannot be modified
Tencent cloud wecity Hello 2022!
VB learning notes
Why does Dell always refuse to push the ultra-thin commercial notebook to the extreme?
Matlab rounding
生态护航 云服务商挥起“英特尔大旗”
Introduction to bi-sql wildcards
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
Bi-sql like
Transform BeanUtils to achieve list data copy gracefully
Reading notes at night -- deep into virtual function
Lenovo tongfuyao: 11 times the general trend, we attacked the city and pulled out the stronghold all the way
Ideas and examples of divide and conquer
leetcode:2104. 子数组范围和