当前位置:网站首页>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);
}
}
};
边栏推荐
- JS delete the objects in the array and specify to delete the objects
- go channel && select
- Complete TCP forwarding server (kernel linked list + mutex)
- For loop and promise to solve the problem of concurrent callback
- DefCamp Capture the Flag (D-CTF) 2021-22 web
- The difference between settimeout() and setinterval()
- Laravel8 custom log directory, rename
- Detailed explanation of the first three passes of upload Labs
- PS cutting height 1px, Y-axis tiling background image problem
- How to use Alibaba Vector Icon
猜你喜欢

Error on datetime when importing SQL file from MySQL
![[buuctf] [geek challenge 2019] secret file](/img/00/23bebd013eb4035555c0057725e3c4.jpg)
[buuctf] [geek challenge 2019] secret file

Introduction to the construction and development of composer private warehouse

Initial attack and defense world Misc

Clear the route cache in Vue

2021-05-12

XSS challenge (6-10) more detailed answers
![[extensive reading of papers] multimodal attribute extraction](/img/ec/546c107ac0d31deded7ca94fdf0e2d.jpg)
[extensive reading of papers] multimodal attribute extraction

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

Geoffreyhinton: my 50 years of in-depth study and Research on mental skills
随机推荐
Invalid argument during startup: Failed to open the . conf file: redis-window
ctfshow nodejs
PHP 2D array change key name
Component communication mode
An error is reported when installing dataspherestudio doc: invalid default value for 'update_ time‘
After the MySQL service on the local computer is started and stopped, some services will automatically stop when they are not used by other services or programs
KnightCTF WEB
XSS challenge (6-10) more detailed answers
DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703
August 24, 2021 deque queue and stack
PHP excel export function encapsulation (based on phpexcel class)
Details of gets, fgetc, fgets, Getc, getchar, putc, fputc, putchar, puts, fputs functions
remote: Support for password authentication was removed on August 13, 2021. Please use a personal ac
[buuctf] [geek challenge 2019] secret file
Lfi-rce without controllable documents
Implementation of forwarding server using IO multiplexing
Initial attack and defense world Misc
ot initialized – call ‘refresh’ before invoking lifecycle methods via the context: Root WebApplicati
Clear the route cache in Vue
Computer screenshot how to cut the mouse in