LeetCode 23-Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

分析

合并k個已排序的鏈表,并分析時間復雜度。k為可大可小的參數,表示需要合并的鏈表數目。
自定義一個Heap類:

  • vector<ListNode*> heap成員變量作為最小堆。
  • vector<ListNode*> indexHeap記錄最小堆內各元素來自于lists中的那個鏈表。與最小堆heap內的元素一一對應。
  • Heap(vector<ListNode*>& lists);構造Heap對象,抽取lists各鏈表頭部的第一個元素。
  • void push(ListNode*, int);heap中插入一個元素,對應地向indexHeap中插入其鏈表編號。
  • int pop();刪除堆頂元素并調整堆,同時返回刪除元素所在的鏈表編號,下一步可以在該鏈表中抽取下一個元素。
  • ListNode* front();返回堆頂元素(不刪除)。
  • bool empty();返回堆是否為空。
  • adjustUp(int index);將堆中編號為index的元素向上調整。
  • adjustDown(int index);將堆中編號為index的元素向下調整。

時間復雜度:設第i個鏈表的長度為ni,則時間復雜度為O(sum(ni)*logk)。每從堆中取出一個節(jié)點,需要O(logk)調整堆,最壞情況總共需要sum(ni)次調整。

注意

  • lists中某鏈表已經完全插入到合并鏈表中時,應避免向堆中插入NULL
  • adjustDown中循環(huán)體應注意s != index,否則會進入死循環(huán),詳見代碼。

AC代碼

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

class Heap {
public:
    Heap(vector<ListNode*>& lists) {
        for (int i = 0; i != lists.size(); ++i) {
            if (lists[i]) {
                push(lists[i], i);
                lists[i] = lists[i]->next;
            }
        }
        // for (int i = heap.size() - 1; i >= 0; --i) {
        //  adjustDown(i);
        // }
    }
    int size() {
        return heap.size();
    }
    bool empty() {
        return heap.empty() && indexHeap.empty();
    }
    void push(ListNode* pointer, int index) {
        heap.push_back(pointer);
        indexHeap.push_back(index);
        adjustUp(heap.size() - 1);
    }
    int pop() {
        int index = indexHeap.front();
        heap.front() = heap.back();
        indexHeap.front() = indexHeap.back();
        heap.pop_back();
        indexHeap.pop_back();
        adjustDown(0);
        return index;
    }
    ListNode* front() {
        return heap.front();
    }
private:
    vector<ListNode*> heap;
    vector<int> indexHeap;
    void adjustDown(int index) {
        if (heap.empty()) {
            return;
        }
        ListNode* save = heap[index];
        int saveIndex = indexHeap[index];
        for (int s = index * 2 + 1; s < heap.size(); s = s * 2 + 1) {
            if (s + 1 < heap.size() && heap[s]->val > heap[s+1]->val) {
                s += 1;
            }
            if (heap[s]->val < save->val) {
                heap[index] = heap[s];
                indexHeap[index] = indexHeap[s];
                index = s;
            } else {
                break;
            }
        }
        heap[index] = save;
        indexHeap[index] = saveIndex;
    }
    void adjustUp(int index) {
        ListNode* save = heap[index];
        int saveIndex = indexHeap[index];
        for (int s = (index - 1) / 2; s >= 0 && s != index; s = (s - 1) / 2) {
            if (heap[s]->val > save->val) {
                heap[index] = heap[s];
                indexHeap[index] = indexHeap[s];
                index = s;
            } else {
                break;
            }
        }
        heap[index] = save;
        indexHeap[index] = saveIndex;
    }
};

class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        ListNode* root = NULL, *current;
        Heap heap(lists);
        while (!heap.empty()) {
            if (root) {
                current->next = heap.front();
                current = current->next;
            } else {
                root = current = heap.front();
            }
            int index = heap.pop();
            if (lists[index]) {
                heap.push(lists[index], index);
                lists[index] = lists[index]->next;
            }
        }
        return root;
    }
};

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容