Website: https://leetcode.com/problems/median-of-two-sorted-arrays/
Test instructions
Gave two sets of ordered numbers to find out their total median
Tips:
The median is the value of the equal number of left and right, and if it does not exist, the average of the two numbers for the number of equal numbers.
Solution 1:
Combine two sets of numbers into a group of numbers. It can also be O (m+n).
But the spatial complexity is O (m+n).
There is obviously a better solution.
Solution 2:
Because the two groups of numbers are already in order, it is assumed to be a group of numbers, with only two subscripts.
Each time by the subscript, the comparison of the two groups of the subscript represented by the value of the order, find the median, the end.
Note that you need to record two values, because the median may be the average of two numbers.
In this way, the spatial complexity is O (1), and the time complexity should be less than O (m+n).
Code:
Https://github.com/LiLane/leetcode/blob/master/java/004-MedianofTwoSortedArrays-201504151322.java
Https://github.com/LiLane/leetcode/blob/master/c%2B%2B/004-MedianofTwoSortedArrays-201504151310.cpp
[Leetcode]-004-median of Sorted Arrays