Ctrl K

Wire Two Panels

Hard
There are two patch panels: the left one has m sockets and the right one has n, with m ≥ n. A cable between left socket i and right socket j costs price[i][j]. You may run any number of cables, and one socket may take several of them. Every socket on both panels must end up with at least one cable. Return the minimum total cost of such wiring.
Example 1
price=[[4, 1], [2, 7]] → 3

Connect left socket 0 to right socket 1 (1) and left socket 1 to right socket 0 (2): 3 in total.

Example 2
price=[[3], [5], [1]] → 9

There is a single right socket, and every left socket has to connect to it: 3 + 5 + 1 = 9.

Example 3
price=[[2, 9, 4], [5, 1, 8], [6, 6, 3]] → 6

Cables along the diagonal: 2 + 1 + 3 = 6. It cannot be cheaper, since that is the sum of the cheapest cable for each right socket.

Constraints
  • m = len(price), n = len(price[i])
  • 1 ≤ n ≤ m ≤ 12
  • 0 ≤ price[i][j] ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsExam Seating Without Peeking HardOne Pick per Shelf HardSettle Up in the Fewest Transfers HardTwo Groups With the Same Average HardAssembling Fragments Hard
loading Python…
Input
price=[[4, 1], [2, 7]]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 9 tests on the server, hidden ones included.