Rebase order
HardOnline Assessment
Find the cheapest order in which to merge a set of feature branches.
A team has n feature branches, numbered 0 to n - 1, and must merge every one of them into the main line, one at a time, in some order of their choosing. When a branch is merged, it has to be rebased against every branch that was merged before it, and each of those earlier branches adds its own conflict-resolution effort. Specifically, merging branch i when the set S of branches is already merged costs the sum of clash[i][j] over all j in S. Merging into an empty main line costs 0.
Note that clash is not necessarily symmetric: clash[i][j] is the effort branch i pays for coming after branch j, and clash[j][i] is what j pays for coming after i. The diagonal entries are 0.
Input: clash, an n x n list of lists of non-negative integers.
Output: an integer, the smallest possible total effort over all n! merge orders.
With n up to 14 there are far too many orders to try one by one.
Example 1
- Input:
- [[[0,5],[2,0]]]
- Output:
- 2
- Explanation
- Merge branch 0 first (free), then branch 1, which pays
clash[1][0] = 2. The reverse order would payclash[0][1] = 5. The answer is 2.
Example 2
- Input:
- [[[0,4,9],[1,0,3],[6,2,0]]]
- Output:
- 9
- Explanation
- Merging in the order 0, 1, 2 costs 0, then
clash[1][0] = 1, thenclash[2][0] + clash[2][1] = 6 + 2 = 8, a total of 9. The other five orders cost 10, 12, 13, 13 and 15, so 9 is the minimum.
Example 3
- Input:
- [[[0,0,0],[0,0,0],[0,0,0]]]
- Output:
- 0
- Explanation
- Every clash is 0, so every order costs 0.
Constraints
1 <= n <= 14, wherenis the number of rows ofclashclashisn x n,0 <= clash[i][j] <= 100, andclash[i][i] = 0