forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0004-median-of-two-sorted-arrays.c
62 lines (51 loc) · 1.59 KB
/
0004-median-of-two-sorted-arrays.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
Time: log(min(n, m))
Space: O(1)
*/
#define min(x, y) ((x < y) ? (x) : (y))
#define max(x, y) ((x > y) ? (x) : (y))
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size){
int* A = nums1;
int* B = nums2;
int ASize = nums1Size;
int BSize = nums2Size;
int total = nums1Size + nums2Size;
int half = total / 2;
if(nums2Size < nums1Size)
{
A = nums2;
B = nums1;
ASize = nums2Size;
BSize = nums1Size;
}
int l = 0;
int r = ASize - 1;
while(true)
{
int i = l + ((r - l - 2 + 1) / 2); // A Mid, round down instead of rounding towards 0
int j = half - i - 2; // B Mid
int Aleft = i >= 0 ? A[i] : INT_MIN;
int Aright = (i + 1) < ASize ? A[i + 1] : INT_MAX;
int Bleft = j >= 0 ? B[j] : INT_MIN;
int Bright = (j + 1) < BSize ? B[j + 1] : INT_MAX;
// partition is correct
if(Aleft <= Bright && Bleft <= Aright)
{
// Odd
if(total % 2)
{
return min(Aright, Bright);
}
// Even
return (max(Aleft, Bleft) + min(Aright, Bright)) / 2.0;
}
else if(Aleft > Bright)
{
r = i - 1;
}
else
{
l = i + 1;
}
}
}