Problem solution · Python

Minimum Cost to Merge Sorted Lists

Minimum Cost to Merge Sorted Lists: a Python solution using binary search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Binary search
Source
Kamyu LeetCode Solutions
Length
59 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Binary search

For Minimum Cost to Merge Sorted Lists, the implementation exploits a monotonic condition to discard half of the remaining search space after every check.

  1. Identify the ordered answer range or sorted search domain.
  2. Write a predicate whose truth changes only once.
  3. Move the appropriate boundary after each midpoint check and return the final feasible position.

Code notes

  • 59 lines of Python from the credited upstream file minimum-cost-to-merge-sorted-lists.py.
  • The implementation visibly relies on sequence storage, work queue.
  • No explicit loop blocks detected.

Complexity

Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeMinimum Cost to Merge Sorted Lists · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(l * nlogn + 2^n * log(n * l) * n * logl + 3^n), n = len(lists), l = max(len(list) for list in lists)# Space: O(n * l + 2^n) import heapqimport bisect  # dp, sort, heap, binary search, submask enumerationclass Solution(object):    def minMergeCost(self, lists):        """        :type lists: List[List[int]]        :rtype: int        """        INF = float("inf")        def merge(lists):            result = []            min_heap = [(lists[i][0], i, 0) for i in xrange(len(lists))]            heapq.heapify(min_heap)            while min_heap:                x, i, j = heapq.heappop(min_heap)                result.append(x)                if j+1 < len(lists[i]):                    heapq.heappush(min_heap, (lists[i][j+1], i, j+1))            return result         def binary_search(left, right, check):            while left <= right:                mid = left+(right-left)//2                if check(mid):                    right = mid-1                else:                    left = mid+1            return left         def check(x):            return sum(bisect.bisect_right(lists[i], sorted_vals[x]) for i in range(len(lists)) if mask&(1<<i)) >= (dp1[mask]+1)//2         dp1 = [0]*(1<<len(lists))        for i in xrange(len(lists)):  # Time: O(2^n)            dp1[1<<i] = len(lists[i])        for mask in xrange(1, len(dp1)):  # Time: O(2^n)            dp1[mask] = dp1[mask^(mask&-mask)]+dp1[mask&-mask]        sorted_vals = merge(lists)  # Time: O(l * nlogn)        sorted_vals = [sorted_vals[i] for i in xrange(len(sorted_vals)) if i+1 == len(sorted_vals) or sorted_vals[i+1] != sorted_vals[i]]        dp2 = [0]*(1<<len(lists))        for mask in xrange(1, len(dp2)):  # Time: O(2^n * log(n * l) * n * logl)            dp2[mask] = sorted_vals[binary_search(0, len(sorted_vals)-1, check)]        dp3 = [0]*(1<<len(lists))        for mask in xrange(1, len(dp3)):  # Time: O(3^n)            if mask&(mask-1) == 0:                continue            dp3[mask] = INF            submask = (mask-1)&mask            while submask > mask^submask:                dp3[mask] = min(dp3[mask], dp3[submask]+dp3[mask^submask]+abs(dp2[submask]-dp2[mask^submask])+dp1[mask])                submask = (submask-1)&mask        return dp3[-1] 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗