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
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
A
andB
to refer tonums1
andnums2
, respectively. These variables are used to represent the two input arrays.Calculate the total length of the combined arrays
nums1
andnums2
and store it in thetotal
variable.Calculate the index
half
which represents the middle position of the combined arrays by dividingtotal
by 2 using floor division (//).Compare the lengths of
nums1
andnums2
. If the length ofnums2
is less than the length ofnums1
, swap the values ofA
andB
. This ensures thatA
always 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 arrayA
minus 1.Enter a loop that continues indefinitely (since there is a
while True
statement).Calculate the midpoint index
i
as the floor division of the sum ofl
andr
by 2. This represents the partition position for arrayA
.Calculate the corresponding partition position
j
for arrayB
by subtractingi
and 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 (
A
andB
) at the partition positionsi
andj
, respectively. Ifi
orj
is less than 0, it means there are no elements to the left, so we assign negative infinity (float("-infinity")
) to the respective variable. Ifi + 1
orj + 1
exceeds 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 <= Bright
andBleft <= Aright
hold 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 betweenAright
andBright
as the median.If the total number of elements is even (
total % 2 == 0
), we return the average of the maximum value betweenAleft
andBleft
and the minimum value betweenAright
andBright
as the median.If the partition is not correct and
Aleft
is greater thanBright
, it means the partition is too far to the right in arrayA
. In this case, we update the right pointer (r
) toi - 1
to search for a new partition position in the left half of arrayA
.If the partition is not correct and
Bleft
is greater thanAright
, it means the partition is too far to the left in arrayA
. In this case, we update the left pointer (l
) toi + 1
to 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