当前位置:网站首页>在两个有序数组中找到整体第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]);
}
}
边栏推荐
- yasea apk 下载 镜像
- void* 指针
- PHP easywechat and applet realize long-term subscription message push
- MySQL multi condition matching fuzzy query
- Hands on data analysis data modeling and model evaluation
- Convert MySQL query timestamp to date format
- AutoCAD - two extension modes
- 戴尔为何一直拒绝将商用本的超薄推向极致?
- 新一代可级联的以太网远程I/O数据采集模块
- Assembly language (4) function transfer parameters
猜你喜欢
随机推荐
动手学数据分析 数据建模和模型评估
天书夜读笔记——内存分页机制
欢迎来到联想智能大屏的新世界
"One good programmer is worth five ordinary programmers!"
期望与方差
4 ans d'expérience de travail, 5 modes de communication Multi - thread ne peuvent pas être décrits, vous osez croire?
Hands on data analysis data modeling and model evaluation
Bi skill - judge 0 and null
Convert MySQL query timestamp to date format
Bi-sql like
天书夜读笔记——反汇编引擎xde32
Bi-sql between
Texture enhancement
Contentresolver, get the SMS content
sql 聚合函数有哪些
Properties of DOM
Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source! (high performance + high customization)
MySQL gets the primary key and table structure of the table
Linux64Bit下安装MySQL5.6-不能修改root密码
【实用系列】家内wifi全覆盖









