parent
a92c066b09
commit
b0e22a5fc4
@ -0,0 +1,48 @@
|
||||
package medianofarrays
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
func median(xs []int) float64 {
|
||||
half := float64(len(xs)) / 2
|
||||
if len(xs)%2 == 0 {
|
||||
return float64(xs[int(half)]+xs[int(half-1)]) / 2
|
||||
}
|
||||
|
||||
return float64(xs[int(half)])
|
||||
}
|
||||
|
||||
func sever(xs []int) (head int, tail []int) {
|
||||
if len(xs) > 0 {
|
||||
return xs[0], xs[1:]
|
||||
}
|
||||
return math.MaxInt, xs
|
||||
}
|
||||
|
||||
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
|
||||
if len(nums1) == 0 && len(nums2) == 0 {
|
||||
return math.NaN()
|
||||
}
|
||||
combined := make([]int, len(nums1)+len(nums2))
|
||||
|
||||
for i := range combined {
|
||||
lhead, ltail := sever(nums1)
|
||||
rhead, rtail := sever(nums2)
|
||||
|
||||
if lhead < rhead {
|
||||
combined[i] = lhead
|
||||
nums1 = ltail
|
||||
} else {
|
||||
combined[i] = rhead
|
||||
nums2 = rtail
|
||||
}
|
||||
}
|
||||
|
||||
return median(combined)
|
||||
}
|
||||
|
||||
func FindMedianSortedArrays(nums1 []int, nums2 []int) float64 {
|
||||
return findMedianSortedArrays(nums1, nums2)
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package medianofarrays_test
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"git.jxs.me/leetgo/medianofarrays"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestEmpty(t *testing.T) {
|
||||
actual := medianofarrays.FindMedianSortedArrays([]int{}, []int{})
|
||||
require.InDelta(t, math.NaN(), actual, 0)
|
||||
}
|
||||
|
||||
func TestRightEmpty(t *testing.T) {
|
||||
actual := medianofarrays.FindMedianSortedArrays([]int{1, 2, 3}, []int{})
|
||||
require.Equal(t, 2.0, actual)
|
||||
}
|
||||
|
||||
func TestLeftEmpty(t *testing.T) {
|
||||
actual := medianofarrays.FindMedianSortedArrays([]int{4, 5, 6}, []int{})
|
||||
require.Equal(t, 5.0, actual)
|
||||
}
|
||||
|
||||
func TestEven(t *testing.T) {
|
||||
actual := medianofarrays.FindMedianSortedArrays([]int{1, 3, 5}, []int{7, 11, 13})
|
||||
require.Equal(t, 6.0, actual)
|
||||
}
|
||||
|
||||
func TestOverlap(t *testing.T) {
|
||||
actual := medianofarrays.FindMedianSortedArrays([]int{1, 7, 23}, []int{1, 7, 11, 17})
|
||||
require.Equal(t, 7.0, actual)
|
||||
}
|
||||
|
||||
func TestExample1(t *testing.T) {
|
||||
actual := medianofarrays.FindMedianSortedArrays([]int{1, 2}, []int{3, 4})
|
||||
require.Equal(t, 2.5, actual)
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
4. Median of Two Sorted Arrays
|
||||
Hard
|
||||
|
||||
Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
|
||||
|
||||
The overall run time complexity should be O(log (m+n)).
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: nums1 = [1,3], nums2 = [2]
|
||||
Output: 2.00000
|
||||
Explanation: merged array = [1,2,3] and median is 2.
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: nums1 = [1,2], nums2 = [3,4]
|
||||
Output: 2.50000
|
||||
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
nums1.length == m
|
||||
nums2.length == n
|
||||
0 <= m <= 1000
|
||||
0 <= n <= 1000
|
||||
1 <= m + n <= 2000
|
||||
-106 <= nums1[i], nums2[i] <= 106
|
||||
|
Loading…
Reference in new issue