【题意】:
Give a divide and conquer algorithm for the following problem: you are given two sorted lists of size m and n, and are allowed unit time access to the ith element of each list. Give an O(lg m + lgn) time algorithm for computing the kth largest element in the union of the two lists. (For simplicity, you can assume that the elements of the two lists are distinct).
【分析】:首先看到这个时间复杂度会想到二分查找那个算法,本题跟二分查找相像但是却有些不同。
首先这两个序列是排序好的(如果不是按照降序排列将其调整为按照降序排列),那么我们去两个序列的中位数A[p]、B[q]进行比较,分为两种情况:
A[p]>=B[q]和
A[p]<B[q],这两种情况是对称的,我们只分析其中一种,另一种对称反转就好了(设A的元素个数为m,B的元素个数为n)。
对于
A[p]>=B[q]:
如果k>=p+q,那么要找的数存在于A[p+1]~A[m]和B中,注意,此处只能将A数组元素个数减半,因为不能确保B中位数之前的元素