File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ double find (vector<int >& nums1, vector<int >& nums2)
4+ {
5+ int n = nums1.size (),m = nums2.size ();
6+
7+ int l = 0 ,h = n;
8+ while (l<=h)
9+ {
10+ int part1= (l+h)/2 ;
11+ int part2=(n+m+1 )/2 - part1;
12+
13+ int l1=(part1==0 )? INT_MIN: nums1[part1-1 ];
14+ int r1= (part1==n)? INT_MAX: nums1[part1];
15+ int l2=(part2==0 )? INT_MIN: nums2[part2-1 ];
16+ int r2= (part2==m)? INT_MAX: nums2[part2];
17+
18+ if (l1<=r2 && l2<=r1)
19+ {
20+ if ((n+m)%2 )
21+ return (double )max (l1,l2);
22+ else
23+ {
24+ return (double )(max (l1,l2)+ min (r1,r2))/2.0 ;
25+ }
26+ }
27+
28+ else if (l1>r2)
29+ h=part1-1 ;
30+ else
31+ l=part1+1 ;
32+ }
33+ return 0 ;
34+ }
35+ double findMedianSortedArrays (vector<int >& nums1, vector<int >& nums2) {
36+ int n=nums1.size (),m=nums2.size ();
37+ if (n>m)return find (nums2,nums1);
38+ else return find (nums1,nums2);
39+
40+ }
41+
42+ };
You can’t perform that action at this time.
0 commit comments