Created
December 2, 2020 21:14
-
-
Save dabsclement/116a3a497a85cd34c45d584fa4ffbe0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 { | |
p1l, p1r := 0, len(nums1) | |
p2l, p2r := 0, len(nums2) | |
m := float64(p1r+p2r-1) / 2 | |
for i := int(m); i > 0; i-- { | |
if p1l == p1r { | |
p2l++ | |
} else if p2l == p2r { | |
p1l++ | |
} else if nums1[p1l] < nums2[p2l] { | |
p1l++ | |
} else { | |
p2l++ | |
} | |
} | |
var sum, t float64 | |
if m == float64(int(m)) { | |
t = 1 // m | |
} else { | |
t = 2 // m, m+1 | |
} | |
for i := int(t); i > 0; i-- { | |
if p1l == p1r { | |
sum += float64(nums2[p2l]) | |
p2l++ | |
} else if p2l == p2r { | |
sum += float64(nums1[p1l]) | |
p1l++ | |
} else if nums1[p1l] < nums2[p2l] { | |
sum += float64(nums1[p1l]) | |
p1l++ | |
} else { | |
sum += float64(nums2[p2l]) | |
p2l++ | |
} | |
} | |
return sum / t | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment