Last active
August 31, 2021 07:49
-
-
Save lienista/d17ae38f3a52b3876fb71adc02269ed8 to your computer and use it in GitHub Desktop.
Algorithms in Javascript: Leetcode 4. Median of Two Sorted Arrays - There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). You may assume nums1 and nums2 cannot be both empty.
This file contains hidden or 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
| const median = (a1, a2) => { | |
| let x = a1.concat(a2); | |
| x.sort(function (a,b) { | |
| return a - b; | |
| }); | |
| let len = x.length; | |
| return len%2 === 0 ? (x[Math.floor(len/2)-1] + x[Math.ceil(len/2)])/2 : x[Math.floor(len/2)]; | |
| } | |
| let a = [0,2,3,5,9]; | |
| let b = [1,4]; | |
| console.log(median(a,b)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment