当前位置:网站首页>Finding the median of two arrays by dichotomy
Finding the median of two arrays by dichotomy
2022-06-30 14:40:00 【Douglas_ LT】
A daily topic ing, Today is a day hard topic 4. Median of Two Sorted Arrays
Method 1 : Merge two arrays , Time complexity O(m+n), Spatial complexity O(m+n)
class Solution {
public:
int findMin(vector<int>& nums) {
int l=0,r=nums.size()-1,min=nums[0];
while(l<=r){
int mid=(l+r)/2;
if(nums[mid]<min){
min=nums[mid];
}
if(nums[l]==nums[mid]){
l++;
continue;
}
if(nums[l]<nums[mid]){
if(nums[l]<min){
min=nums[l];
}
l=mid+1;
}
else{
if(nums[mid]<min){
min=nums[mid];
}
r=mid-1;
}
}
return min;
}
};
Method 2 : Dichotomy , Time complexity O(log(m+n)), Spatial complexity O(1)
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int m=nums1.size(),n=nums2.size();
int left=(m+n+1)/2,right=(m+n+2)/2;
return (findK(nums1,0,nums2,0,left)+findK(nums1,0,nums2,0,right))/2.0;
}
int findK(vector<int>& nums1,int i,vector<int>& nums2,int j,int k){
int m=nums1.size(),n=nums2.size();
if(i>=m){
return nums2[j+k-1];
}
if(j>=n){
return nums1[i+k-1];
}
if(k==1){
return min(nums1[i],nums2[j]);
}
int mid1=(i+k/2-1>=m)?INT_MAX:nums1[i+k/2-1];
int mid2=(j+k/2-1>=n)?INT_MAX:nums2[j+k/2-1];
if(mid1>mid2){
return findK(nums1,i,nums2,j+k/2,k-k/2);
}
else{
return findK(nums1,i+k/2,nums2,j,k-k/2);
}
}
};
边栏推荐
- Wuenda 2022 machine learning special course evaluation is coming!
- [extensive reading of papers] sentimental analysis of online reviews with a hierarchical attention network
- Summary of use of laravel DCAT admin
- V3 03_ Getting started
- Implementation of forwarding server using IO multiplexing
- PHP 2D array change key name
- Go language func function
- Alipay certificate mode payment interface
- Lihongyi machine learning 2020 homework summary
- [geek challenge 2019] PHP problem solving record
猜你喜欢

go time. after

ThinkPHP show method parameter controllable command execution

ctfshow nodejs

Solve the error in my QT_ thread_ global_ End(): 3 threads didn't exit

LIS error: this configuration section cannot be used in this path

The first dark spring cup dnuictf

MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video

DiceCTF - knock-knock

Initial attack and defense world Misc

Shell programming overview
随机推荐
Mysql database foundation: stored procedures and functions
Details of gets, fgetc, fgets, Getc, getchar, putc, fputc, putchar, puts, fputs functions
August 24, 2021 deque queue and stack
Wuenda 2022 machine learning special course evaluation is coming!
How does hbuilder display in columns?
Notepad regular delete the line of the keyword
Attack and defense world web questions
【BUUCTF】 Have Fun
Begin End use the pit encountered
PHP conditional operator
Att & CK red team evaluation field (I)
Uniapp upload image method
@ResponseBody的作用
中信期货开户麻烦吗安全吗,期货开户手续费是多少,能优惠吗
Is it troublesome for CITIC futures to open an account? Is it safe? How much is the handling charge for opening an account for futures? Can you offer a discount
Zend studio how to import an existing project
JS time conversion standard format, timestamp conversion standard format
[extensive reading of papers] multimodal joint attribute prediction and value extraction for e-commerce product
Knowledge learned from the water resources institute project
Laravel upload error