-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0004_median-of-two-sorted-arrays.py
76 lines (67 loc) · 2.56 KB
/
0004_median-of-two-sorted-arrays.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from typing import List
# ----------------------------------
# Two pointer method
# ----------------------------------
# Status: Accepted (2094/2094)
# Runtime: 82 ms, faster than 90.40% of Python3 submissions.
# Memory Usage: 16.7 MB, less than 16.68% of Python3 submissions.
# class Solution:
# def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
# N: int = len(nums1)
# M: int = len(nums2)
# result_len: int = len(nums1) + len(nums2)
# is_odd: bool = result_len % 2 == 1
# upper_bound: int = result_len // 2 if is_odd else (result_len // 2)
# i: int = 0
# j: int = 0
# k: int = 0
# out: List[int] = []
# while (i < N or j < M) and (k < (upper_bound + 1)):
# k += 1
# if i == N:
# out.append(nums2[j])
# j += 1
# elif j == M:
# out.append(nums1[i])
# i += 1
# elif nums1[i] < nums2[j]:
# out.append(nums1[i])
# i += 1
# else:
# out.append(nums2[j])
# j += 1
# return out[-1] if is_odd else ((out[-1] + out[-2]) / 2)
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
len1 = len(nums1)
len2 = len(nums2)
if len1 > len2:
return self.findMedianSortedArrays(nums2, nums1)
left = 0
right = len1
out_median_idx = (len1 + len2 + 1) // 2
# Basically binary search using two windows
while left <= right:
m = (left + right) // 2
m1 = m
m2 = out_median_idx - m1
left_partition1 = nums1[m1 - 1] if m1 > 0 else float("-inf")
left_partition2 = nums2[m2 - 1] if m2 > 0 else float("-inf")
right_partition1 = nums1[m1] if m1 < len1 else float("inf")
right_partition2 = nums2[m2] if m2 < len2 else float("inf")
if (
left_partition1 <= right_partition2
and left_partition2 <= right_partition1
):
if (len1 + len2) % 2 == 0:
return (
max(left_partition1, left_partition2)
+ min(right_partition1, right_partition2)
) / 2.0
else:
return max(left_partition1, left_partition2)
elif left_partition1 > right_partition2:
right = m - 1
else:
left = m + 1
return 0.0