Find the top k sums of two sorted arrays

前端 未结 4 2008
独厮守ぢ
独厮守ぢ 2021-02-06 01:33

You are given two sorted arrays, of sizes n and m respectively. Your task (should you choose to accept it), is to output the largest k sums of the form a[i]+b[j].

4条回答
  •  渐次进展
    2021-02-06 02:13

    I found the responses at your link mostly vague and poorly structured. Here's a start with a O(k * log(min(m, n))) O(k * log(m + n)) O(k * log(k)) algorithm.

    Suppose they are sorted decreasing. Imagine you computed the m*n matrix of the sums as follows:

    for i from 0 to m
        for j from 0 to n
            sums[i][j] = a[i] + b[j]
    

    In this matrix, values monotonically decrease down and to the right. With that in mind, here is an algorithm which performs a graph search through this matrix in order of decreasing sums.

    q : priority queue (decreasing) := empty priority queue
    add (0, 0) to q with priority a[0] + b[0]
    while k > 0:
        k--
        x := pop q
        output x
        (i, j) : tuple of int,int := position of x
        if i < m:
            add (i + 1, j) to q with priority a[i + 1] + b[j]
        if j < n:
            add (i, j + 1) to q with priority a[i] + b[j + 1]
    

    Analysis:

    1. The loop is executed k times.
      1. There is one pop operation per iteration.
      2. There are up to two insert operations per iteration.
    2. The maximum size of the priority queue is O(min(m, n)) O(m + n) O(k).
    3. The priority queue can be implemented with a binary heap giving log(size) pop and insert.
    4. Therefore this algorithm is O(k * log(min(m, n))) O(k * log(m + n)) O(k * log(k)).

    Note that the general priority queue abstract data type needs to be modified to ignore duplicate entries. Alternately, you could maintain a separate set structure that first checks for membership in the set before adding to the queue, and removes from the set after popping from the queue. Neither of these ideas would worsen the time or space complexity.

    I could write this up in Java if there's any interest.

    Edit: fixed complexity. There is an algorithm which has the complexity I described, but it is slightly different from this one. You would have to take care to avoid adding certain nodes. My simple solution adds many nodes to the queue prematurely.

提交回复
热议问题