Skip to content

Commit

Permalink
[python]median-of-two-sorted-arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Huauauaa committed Aug 18, 2024
1 parent 0d921c5 commit 2fa5a98
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/leetcode/00004.median-of-two-sorted-arrays/4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import List


class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
res = nums1[::]

def inset(t):
left = 0
right = len(res) - 1
while left <= right:
mid = (left + right) >> 1
if res[mid] > t:
right = mid - 1
else:
left = mid + 1
res.insert(left, t)

for i in nums2:
inset(i)

length = len(res)
if length % 2 == 0:
return (res[length // 2 - 1] + res[length // 2]) / 2
else:
return res[length // 2]


if __name__ == "__main__":
res = Solution().findMedianSortedArrays([1, 2], [3, 4])
print(res)

0 comments on commit 2fa5a98

Please sign in to comment.