3801. Minimum Cost to Merge Sorted Lists
Description
You are given a 2D integer array lists, where each lists[i] is a non-empty array of integers sorted in non-decreasing order.
You may repeatedly choose two lists a = lists[i] and b = lists[j], where i != j, and merge them. The cost to merge a and b is:
len(a) + len(b) + abs(median(a) - median(b)), where len and median denote the list length and median, respectively.
After merging a and b, remove both a and b from lists and insert the new merged sorted list in any position. Repeat merges until only one list remains.
Return an integer denoting the minimum total cost required to merge all lists into one single sorted list.
The median of an array is the middle element after sorting it in non-decreasing order. If the array has an even number of elements, the median is the left middle element.
Example 1:
Input: lists = [[1,3,5],[2,4],[6,7,8]]
Output: 18
Explanation:
Merge a = [1, 3, 5] and b = [2, 4]:
len(a) = 3andlen(b) = 2median(a) = 3andmedian(b) = 2cost = len(a) + len(b) + abs(median(a) - median(b)) = 3 + 2 + abs(3 - 2) = 6
So lists becomes [[1, 2, 3, 4, 5], [6, 7, 8]].
Merge a = [1, 2, 3, 4, 5] and b = [6, 7, 8]:
len(a) = 5andlen(b) = 3median(a) = 3andmedian(b) = 7cost = len(a) + len(b) + abs(median(a) - median(b)) = 5 + 3 + abs(3 - 7) = 12
So lists becomes [[1, 2, 3, 4, 5, 6, 7, 8]], and total cost is 6 + 12 = 18.
Example 2:
Input: lists = [[1,1,5],[1,4,7,8]]
Output: 10
Explanation:
Merge a = [1, 1, 5] and b = [1, 4, 7, 8]:
len(a) = 3andlen(b) = 4median(a) = 1andmedian(b) = 4cost = len(a) + len(b) + abs(median(a) - median(b)) = 3 + 4 + abs(1 - 4) = 10
So lists becomes [[1, 1, 1, 4, 5, 7, 8]], and total cost is 10.
Example 3:
Input: lists = [[1],[3]]
Output: 4
Explanation:
Merge a = [1] and b = [3]:
len(a) = 1andlen(b) = 1median(a) = 1andmedian(b) = 3cost = len(a) + len(b) + abs(median(a) - median(b)) = 1 + 1 + abs(1 - 3) = 4
So lists becomes [[1, 3]], and total cost is 4.
Example 4:
Input: lists = [[1],[1]]
Output: 2
Explanation:
The total cost is len(a) + len(b) + abs(median(a) - median(b)) = 1 + 1 + abs(1 - 1) = 2.
Constraints:
2 <= lists.length <= 121 <= lists[i].length <= 500-109 <= lists[i][j] <= 109lists[i]is sorted in non-decreasing order.- The sum of
lists[i].lengthwill not exceed 2000.
Solutions
Solution 1: State Compression DP
Thinking
There are at most \(n \le 12\) lists, so enumerating merge orders as Catalan trees repeats the same subsets. Total length is modest, but the order itself cannot be searched.
Length and median of a merge depend only on the multiset of values, not on the intermediate merge sequence. Each subset therefore has a unique length and median.
We represent unused lists as a bit mask, precompute each nonempty subset's count and left median, then DP by splitting a set into two nonempty proper subsets, adding the median gap and the total length.
The \(2^n\) subset DP covers every collection; the answer is the cost of the full mask.
The number of lists satisfies \(n \le 12\), so a bitmask can represent any subset of lists.
Merging two sorted lists yields the sorted union of their elements, so the length and median of a set of lists depend only on the set itself, not on the merge order. The median is the left middle element after sorting, i.e. the \(\lfloor (len + 1)/2 \rfloor\)-th smallest value.
Precompute for every nonempty subset \(i\):
- \(\textit{cnt}[i]\): the number of elements in the subset;
- \(\textit{med}[i]\): the median of the subset. Binary search over distinct values and count how many elements in the subset are at most \(\textit{mid}\).
Let \(f[i]\) be the minimum cost to merge all lists in subset \(i\) into one list. If \(i\) contains a single list, \(f[i] = 0\). Otherwise enumerate a nonempty proper subset \(j\) of \(i\) and let \(k = i \oplus j\):
The length part of the last merge is always \(\textit{cnt}[i]\). The answer is \(f[2^n - 1]\).
Time complexity is \(O(3^n + 2^n \times n \times \log V \times \log L)\), and space complexity is \(O(2^n)\), where \(n\) is the number of lists, \(V\) is the number of distinct values, and \(L\) is the maximum length of a single list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |