-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC0014.py
46 lines (38 loc) · 1.17 KB
/
LC0014.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
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
median_indices = []
median_values = []
total_len = (len(nums1) + len(nums2))
if total_len % 2 == 0:
median_indices.append(total_len//2 - 1)
median_indices.append(total_len//2)
else:
median_indices.append(total_len//2)
i = 0
j = 0
k = -1
sto = -1
while(i<len(nums1) and j<len(nums2)):
if nums1[i]<=nums2[j]:
sto = nums1[i]
i += 1
k += 1
else:
sto = nums2[j]
j += 1
k += 1
if k in median_indices:
median_values.append(sto)
while(i<len(nums1)):
sto = nums1[i]
i += 1
k += 1
if k in median_indices:
median_values.append(sto)
while(j<len(nums2)):
sto = nums2[j]
j += 1
k += 1
if k in median_indices:
median_values.append(sto)
return sum(median_values)/len(median_values)