In some degree, this problem is similar to median of medians algorithm we talk about in class (I use MOM to represent it in following answers).
The similarity is we can also use median of medians to eliminate impossible portion of lists and then recursively call algorithm. The logic here is straightforward:
- select three medians of 3 lists (m1, m2 and m3 map to n1, n2 and n3) and then sort them. Based on 3 lists have already been sorted, this step only needs constant time to complete. O(1)
- Then next is the primary observation: let's suppose in step 1: m1<m2<m3 . In this case, we can know first half of list n1 and second half of list n3 are eliminated. Here the logic is similar to MOM, because the first half of n1 are less than median of medians then they are impossible to be median of whole merged list. Also the second half of n3 are greater than median of medians then they are also impossible to be candidate. But in order to keep the final median same, we have to eliminate same length of lists on both side. So we need to compare length of n1 and n3 to find one that has smaller length and eliminate this length / 2 on both sides. The only difference within this problem and MOM is, in MOM because we deal with an unsorted list, so we can only make sure the final median is between 3/10 and 7/10 of list and we need to use median of medians to partition and eliminate some numbers. But in this problem, because all lists are sorted, we can eliminate two parts simultaneously. For a summary: every time we compare length of two lists that have smallest median and largest median to find a smaller length, and then eliminate lists with this length / 2 on smallest median list from beginning and largest median list from end. This step also require constant time. O(1)
- Remaining things are easy, we can recursively call algorithm until we find the final median.
Consider the time complexity of this algorithm, let n = n1 + n2 + n3 (this is the size of merged list). every time we call the algorithm we eliminate smaller length / 2 from two lists. But no matter how many numbers are actually eliminated, the remaining part is a number less than n, let's say k*n (k < 1). Plus every time we use constant time to find median of medians and this smaller length. The recurrence relation can then be represented as: T(n) = T(kn) + c, which k < 1. Use master theorem we can easily now the total time complexity is: O(logn).
By the way, if we consider a general case: say we have m sorted list. Then the final complexity should be (m logn).