当前位置:网站首页>O (log (min (m, n))
O (log (min (m, n))
2022-06-25 01:34:00 【N. LAWLIET】
problem :
Find the whole number in two ordered arrays K A small number can do O(log(Min(M,N)))
Their thinking
Explain : Discussion by situation
1)1<=k<= Short array length
2) Long array length <=k<= Overall array length
3) Short array length <=k<= Long array length
Code
public class Code03_FindKthMinNumber {
// Advanced questions : In two arrays that are both ordered , Find the whole page K Small number
// It can be done 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);
}
}
// Compare the median of two equal length arrays
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) {
// Singular case
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]);
}
}
边栏推荐
- “一个优秀程序员可抵五个普通程序员!”
- 屡获大奖的界面控件开发包DevExpress v22.1官宣发布
- Using macro code to generate handwriting automatically in word or WPS
- Introduction to bi-sql wildcards
- String common methods
- 【直播回顾】2022腾讯云未来社区城市运营方招募会暨SaaS 2.0新品发布会!
- Boutique enterprise class powerbi application pipeline deployment
- ICML2022 | 用神经控制微分方程建立反事实结果的连续时间模型
- PHP 利用getid3 获取mp3、mp4、wav等媒体文件时长等数据
- How to prepare for the last day of tomorrow's exam? Complete compilation of the introduction to the second building test site
猜你喜欢

1. package your own scaffold 2 Create code module

海河实验室创新联合体成立 GBASE成为首批创新联合体(信创)成员单位

PMP考试“临门一脚”如何踢得漂亮?

Bi-sql create

js数组对象转对象

动手学数据分析 数据建模和模型评估

同一服务器两个端口不同的应用session覆盖解决方案

Reading notes at night -- deep into virtual function

Boutique enterprise class powerbi application pipeline deployment
![全排列II[存在相同元素去重 + 标准回溯]](/img/d3/93ddb49e580be60be4f056f141b782.png)
全排列II[存在相同元素去重 + 标准回溯]
随机推荐
Tianshu night reading notes -- memory paging mechanism
PMP考试“临门一脚”如何踢得漂亮?
Fan benefits, JVM manual (including PDF)
Linux64Bit下安装MySQL5.6-不能修改root密码
CCNP的BGP部分笔记
PHP 利用getid3 获取mp3、mp4、wav等媒体文件时长等数据
屡获大奖的界面控件开发包DevExpress v22.1官宣发布
Tencent has completed the comprehensive cloud launch to build the largest cloud native practice in China
现状分析:“一云多芯”如何推动信创项目快速部署
Bi-sql Union
Boutique enterprise class powerbi application pipeline deployment
Unity C# 网络学习(六)——FTP(一)
腾讯完成全面上云 打造国内最大云原生实践
How to prepare for the last day of tomorrow's exam? Complete compilation of the introduction to the second building test site
Abnova丨BSG 单克隆抗体中英文说明
腾讯云WeCity解决方案
Install mysql5.6 under linux64bit - the root password cannot be modified
After the college entrance examination, the following four situations will inevitably occur:
Texture enhancement
AssertionError: CUDA unavailable, invalid device 0 requested