Median of Two Sorted Arrays
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)).
Examples​
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 == mnums2.length == n0 <= m <= 10000 <= n <= 10001 <= m + n <= 2000-106 <= nums1[i], nums2[i] <= 106
Solution​
Approach​
To find the median of the two sorted arrays, we first merge the two arrays into one sorted array. Then, based on whether the combined length is even or odd, we calculate the median accordingly.
Pseudocode​
- Merge
nums1andnums2into a single sorted arraynums. - Find the middle index of
nums. - If the length of
numsis even, calculate the median as the average of the two middle elements. - If the length of
numsis odd, the median is the middle element itself. - Return the calculated median.
Code​
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
nums = sorted((nums1+nums2))
median = 0
middle = len(nums)//2
if len(nums)%2==0:
median = (nums[middle]+nums[middle-1])/2
else:
median = nums[middle]
return median
Complexity Analysis​
- Time Complexity: O(m + n + log(m + n)), where m and n are the lengths of
nums1andnums2respectively. This is due to the sorting operation and finding the median. - Space Complexity: O(m + n) for the merged array
nums.
This documentation provides a clear explanation of the problem, the approach used to solve it, the code implementation, and the complexity analysis of the solution.