forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0004-median-of-two-sorted-arrays.go
70 lines (60 loc) · 1.06 KB
/
0004-median-of-two-sorted-arrays.go
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
63
64
65
66
67
68
69
70
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
A, B := nums1, nums2
total := len(nums1) + len(nums2)
half := (total + 1) / 2
var Aleft, Aright float64
var Bleft, Bright float64
if len(B) < len(A) {
A, B = B, A
}
l, r := 0, len(A)-1
for {
i := (l + r) >> 1 // A
j := half - i - 2 // B
if i >= 0 {
Aleft = float64(A[i])
} else {
Aleft = math.Inf(-1)
}
if (i + 1) < len(A) {
Aright = float64(A[i+1])
} else {
Aright = math.Inf(1)
}
if j >= 0 {
Bleft = float64(B[j])
} else {
Bleft = math.Inf(-1)
}
if (j + 1) < len(B) {
Bright = float64(B[j+1])
} else {
Bright = math.Inf(1)
}
// partition is correct
if Aleft <= Bright && Bleft <= Aright {
// odd
if total%2 == 1 {
return max(Aleft, Bleft)
}
// even
return (max(Aleft, Bleft) + min(Aright, Bright)) / 2
} else if Aleft > Bright {
r = i - 1
} else {
l = i + 1
}
}
}
func max(a, b float64) float64 {
if a > b {
return a
}
return b
}
func min(a, b float64) float64 {
if a < b {
return a
}
return b
}