30. Median of Two Sorted Arrays
HardSearching~45 min
Given two sorted arrays, return the median of all their values combined.
If the combined length is even, the median is the average of the two middle values. At least one of the arrays is non-empty.
Examples
Example 1
- Input:
- a = [1, 3], b = [2]
- Output:
- 2
- Why:
- Combined: [1, 2, 3], middle value 2.
Example 2
- Input:
- a = [1, 2], b = [3, 4]
- Output:
- 2.5
- Why:
- Combined: [1, 2, 3, 4], average of 2 and 3.
Constraints
0 <= a.length, b.length <= 1000Both arrays are sorted ascending.a.length + b.length >= 1