Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 23 · Merge

Merge k Sorted Lists

LeetCode 23 · ಕಠಿಣ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

Pairwise merging rounds — O(N log k).

ಕಠಿಣLinked ListDivide and ConquerHeap (Priority Queue)Merge SortTournament Sort
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Merge k Sorted Lists (LeetCode 23) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Merge ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಕಠಿಣ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

Merge lists in pairs, like the levels of merge sort: k lists become k/2, then k/4 … until one remains. Every element is merged about log k times.

Approach

  1. 1

    Reuse the two-list merge.

  2. 2

    Each round merges lists[0]&[1], [2]&[3], … into a new list of lists.

  3. 3

    Repeat until one list is left.

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ

Divide and conquer mergingPythonO(N log k) time
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
        #merge in two and store in lists still the length of the lists>1
        def merge_list(list1,list2):
            dummy = ListNode(-1)
            temp = dummy
            while list1 and list2:
                if list1.val<=list2.val:
                    temp.next = list1
                    temp = temp.next
                    list1=list1.next
                else:
                    temp.next = list2
                    temp = temp.next
                    list2=list2.next
            if not list2:
                temp.next = list1
            if not list1:
                temp.next = list2
            return dummy.next

        n = len(lists)
        if not lists:
            return None
        if n==1:
            return lists[0]
        while len(lists)>1:
            merged_lists_temp=[]
            for i in range(0,len(lists),2):
                list1 = lists[i]
                list2 = lists[i+1] if i+1<len(lists) else None
                merged_list = merge_list(list1,list2)
                merged_lists_temp.append(merged_list)
            lists = merged_lists_temp
        return lists[0]
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Merge k Sorted Lists — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Merge k Sorted Lists ಹಿಂದಿನ intuition ಏನು?

Merge lists in pairs, like the levels of merge sort: k lists become k/2, then k/4 … until one remains. Every element is merged about log k times.

Merge k Sorted Lists (LeetCode 23) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. Reuse the two-list merge. 2. Each round merges lists[0]&[1], [2]&[3], … into a new list of lists. 3. Repeat until one list is left.

Merge k Sorted Lists ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(N log k). Space: O(k) for the round list. (Accepted approach: Divide and conquer merging.)

Merge k Sorted Lists ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

Merging one by one into a growing list is O(N·k).