How to find pair with kth largest sum?

前端 未结 6 877
深忆病人
深忆病人 2020-12-02 12:29

Given two sorted arrays of numbers, we want to find the pair with the kth largest possible sum. (A pair is one element from the first array and one element from the second

6条回答
  •  孤城傲影
    2020-12-02 13:23

    If the last two solutions were at (a1, b1), (a2, b2), then it seems to me there are only four candidate solutions (a1-1, b1) (a1, b1-1) (a2-1, b2) (a2, b2-1). This intuition could be wrong. Surely there are at most four candidates for each coordinate, and the next highest is among the 16 pairs (a in {a1,a2,a1-1,a2-1}, b in {b1,b2,b1-1,b2-1}). That's O(k).

    (No it's not, still not sure whether that's possible.)

提交回复
热议问题