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 == mnums2.length == n0 <= m <= 10000 <= n <= 10001 <= m + n <= 2000-106 <= nums1[i], nums2[i] <= 106
SOLUTION
Time Complexity: O(log(min(n, m))), where n and m are the lengths of the input arrays nums1 and nums2, respectively. The algorithm performs binary search on the smaller array to find the correct partition position, and each iteration of the binary search reduces the search space by half. Therefore, the time complexity is logarithmic with respect to the smaller input size.
Space Complexity:O(1)
How it works:
Initialize variables
AandBto refer tonums1andnums2, respectively. These variables are used to represent the two input arrays.Calculate the total length of the combined arrays
nums1andnums2and store it in thetotalvariable.Calculate the index
halfwhich represents the middle position of the combined arrays by dividingtotalby 2 using floor division (//).Compare the lengths of
nums1andnums2. If the length ofnums2is less than the length ofnums1, swap the values ofAandB. This ensures thatAalways refers to the array with the smaller length.Initialize the left (
l) and right (r) pointers for binary search. The left pointer (l) is set to 0, and the right pointer (r) is set to the length of arrayAminus 1.Enter a loop that continues indefinitely (since there is a
while Truestatement).Calculate the midpoint index
ias the floor division of the sum oflandrby 2. This represents the partition position for arrayA.Calculate the corresponding partition position
jfor arrayBby subtractingiand 2 fromhalf. This ensures that the total number of elements to the left of the partition in both arrays is equal tohalf.Determine the left and right elements for both arrays (
AandB) at the partition positionsiandj, respectively. Ifiorjis less than 0, it means there are no elements to the left, so we assign negative infinity (float("-infinity")) to the respective variable. Ifi + 1orj + 1exceeds the array length, it means there are no elements to the right, so we assign positive infinity (float("infinity")) to the respective variable.Compare the elements to check if the partition is correct. If the conditions
Aleft <= BrightandBleft <= Arighthold true, it means the partition is correct.If the partition is correct, we check if the total number of elements is odd (
total % 2 == 1). If it is, we return the minimum value betweenArightandBrightas the median.If the total number of elements is even (
total % 2 == 0), we return the average of the maximum value betweenAleftandBleftand the minimum value betweenArightandBrightas the median.If the partition is not correct and
Aleftis greater thanBright, it means the partition is too far to the right in arrayA. In this case, we update the right pointer (r) toi - 1to search for a new partition position in the left half of arrayA.If the partition is not correct and
Bleftis greater thanAright, it means the partition is too far to the left in arrayA. In this case, we update the left pointer (l) toi + 1to search for a new partition position in the right half of arrayA.The loop continues until the correct partition position is found or until it breaks if the correct position cannot be found.
Comments
Post a Comment