-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedian-of-two-sorted-arrays.go
More file actions
79 lines (75 loc) · 1.03 KB
/
Copy pathmedian-of-two-sorted-arrays.go
File metadata and controls
79 lines (75 loc) · 1.03 KB
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
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
)
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
if len(nums1) < len(nums2) {
nums1, nums2 = nums2, nums1
}
l1, l2 := len(nums1), len(nums2)
mid := ((l1 + l2) / 2) + 1
v1, v2 := 0, 0
for i, j := 0, 0; i+j < mid; {
if (i < l1 && j < l2 && nums1[i] <= nums2[j]) || j >= l2 {
v1, v2 = v2, nums1[i]
i++
} else {
v1, v2 = v2, nums2[j]
j++
}
}
// println(mid, mid%2, v1, v2)
if (l1+l2)%2 == 0 {
return float64(v1+v2) / 2.0
}
return float64(v2)
}
func main() {
test1 := []int{100001}
test2 := []int{100000}
println("Testing")
fmt.Println(test1)
fmt.Println(test2)
result := findMedianSortedArrays(test1, test2)
println("Yields", result)
}
/*
[-5]
[-4]
[-4]
[-5]
[100000]
[100001]
[100001]
[100000]
[1,6,7,4]
[2,3,5,8]
[1,2,3,4]
[5,6,7,8]
[1,2,3,4]
[5,6,7,8]
[1,2,3,4,9]
[]
[1,9]
[2,3,4,5]
[2,3]
[1,4,5]
[2,3]
[1,4,5]
[1,2]
[3,4]
[3]
[-2,-1]
[1,3]
[2]
[1,3,5,7,9]
[2,4,6,8,10]
[]
[1]
[]
[1,2]
[]
[1,2,3]
[1,2,2,2,2,4,5,6,7,8,9,9,9]
[1,1,1,1,4,4,5,6,6,6,7,7,8,8,9,9]
*/