Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create medianOfTwoSortedArrays.py #42

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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