-
Notifications
You must be signed in to change notification settings - Fork 0
/
median_sorted_arrays.py
51 lines (35 loc) · 1.25 KB
/
median_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
class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
median of two sorted arrays
merge both into an array
count how many additions we're doing to the merged array
once we reach the halfway point then return that
check if even or odd, then calc median
if even
merge two lists until
"""
if (len(nums1)+len(nums2))% 2:
status = "even"
else:
status = "odd"
counter = 0
median = 0
i, j = 0, 0
while i < len(nums1) and j < len(nums2):
if counter == (len(nums2)+len(nums1))/2 - 1:
median = min(nums1[i], nums2[j])
if nums1[i] < nums2[j]:
i+=1
counter+=1
else:
j+=1
counter+=1
if status == "odd":
median = median + min(nums1[i+1], nums2[j+1])
return median
else:
return median