-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMedianofTwoSortedArrays.py
More file actions
executable file
·61 lines (45 loc) · 1.45 KB
/
MedianofTwoSortedArrays.py
File metadata and controls
executable file
·61 lines (45 loc) · 1.45 KB
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
"""
Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
The overall run time complexity should be O(log (m+n)).
Example 1:
Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.
Example 2:
Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
Constraints:
nums1.length == m
nums2.length == n
0 <= m <= 1000
0 <= n <= 1000
1 <= m + n <= 2000
-106 <= nums1[i], nums2[i] <= 106
"""
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
# Merge the two sorted arrays
merged = sorted(nums1 + nums2)
n = len(merged)
# Calculate the median
if n % 2 == 0:
# Even number of elements, average the two middle elements
middle1 = merged[n // 2 - 1]
middle2 = merged[n // 2]
return (middle1 + middle2) / 2.0
else:
# Odd number of elements, return the middle element
return float(merged[n // 2])
# Create an instance of the Solution class
solution = Solution()
# Test Case 1
nums1 = [1, 3]
nums2 = [2]
result1 = solution.findMedianSortedArrays(nums1, nums2)
print(result1) # Expected output: 2.0
# Test Case 2
nums3 = [1, 2]
nums4 = [3, 4]
result2 = solution.findMedianSortedArrays(nums3, nums4)
print(result2) # Expected output: 2.5