Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Median of two sorted arrays
class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { if(nums1.size()>nums2.size()){ return findMedianSortedArrays(nums2,nums1); } int x=nums1.size(); int y=nums2.size(); int partitionX; int partitionY; int low=0; int high=x; while(low<=high) { partitionX=(low+high)/2; partitionY=(x+y+1)/2-partitionX; int maxleftX=(partitionX==0)?INT_MIN:nums1[partitionX-1]; int minrightX=(partitionX==x)?INT_MAX:nums1[partitionX]; int maxleftY=(partitionY==0)?INT_MIN:nums2[partitionY-1]; int minrightY=(partitionY==y)?INT_MAX:nums2[partitionY]; if(maxleftX<=minrightY && maxleftY<=minrightX) { if((x+y)%2==0) { double avg=max(maxleftX,maxleftY)+min(minrightX,minrightY); return avg/2; } else { return (double) max(maxleftX,maxleftY); } } else if(maxleftX>minrightY) { high=partitionX-1; } else { low=partitionX+1; } } double a=0; return a; } };
run
|
edit
|
history
|
help
0
char
Proyecto 1
isBST
Спарсить полученный в виде строки JSON в объект (Boost)
Kth smallest element
Stock buy/sell
sheetal
K edit distance
Graph Theory 2
Two-phase sample with GCC