From b2ca20a40c464432dd661abf246d317ec1627b5c Mon Sep 17 00:00:00 2001 From: Vatsal Dalal <67739998+vatsal9117@users.noreply.github.com> Date: Tue, 4 Oct 2022 10:05:22 +0530 Subject: [PATCH] Create Q4_medianOfTwoSortedArrays.py --- .../Q4_medianOfTwoSortedArrays.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 leetCode Solutions/Q4_medianOfTwoSortedArrays/Q4_medianOfTwoSortedArrays.py diff --git a/leetCode Solutions/Q4_medianOfTwoSortedArrays/Q4_medianOfTwoSortedArrays.py b/leetCode Solutions/Q4_medianOfTwoSortedArrays/Q4_medianOfTwoSortedArrays.py new file mode 100644 index 0000000..9a98833 --- /dev/null +++ b/leetCode Solutions/Q4_medianOfTwoSortedArrays/Q4_medianOfTwoSortedArrays.py @@ -0,0 +1,23 @@ +class Solution: + def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: + n1 = len(nums1) + n2 = len(nums2) + if n1 > n2: + return self.findMedianSortedArrays(nums2, nums1) + + l = 0 + r = n1 + + while l <= r: + partition1 = (l + r) // 2 + partition2 = (n1 + n2 + 1) // 2 - partition1 + maxLeft1 = -2**31 if partition1 == 0 else nums1[partition1 - 1] + maxLeft2 = -2**31 if partition2 == 0 else nums2[partition2 - 1] + minRight1 = 2**31 - 1 if partition1 == n1 else nums1[partition1] + minRight2 = 2**31 - 1 if partition2 == n2 else nums2[partition2] + if maxLeft1 <= minRight2 and maxLeft2 <= minRight1: + return (max(maxLeft1, maxLeft2) + min(minRight1, minRight2)) * 0.5 if (n1 + n2) % 2 == 0 else max(maxLeft1, maxLeft2) + elif maxLeft1 > minRight2: + r = partition1 - 1 + else: + l = partition1 + 1