Minimum Cost to Connect Two Groups of Points - Problem
Minimum Cost to Connect Two Groups of Points

Imagine you're a network engineer tasked with connecting two cities through multiple connection points. You have two groups of points where Group A has size1 points and Group B has size2 points, with the constraint that size1 >= size2.

The connection cost between any two points is given in a size1 x size2 matrix where cost[i][j] represents the cost of connecting point i from Group A to point j from Group B.

The Challenge: Both groups must be fully connected, meaning:
โ€ข Every point in Group A must connect to at least one point in Group B
โ€ข Every point in Group B must connect to at least one point in Group A

Your goal is to find the minimum total cost to achieve this complete connectivity between both groups.

Input & Output

example_1.py โ€” Basic Connection
$ Input: cost = [[15,96],[36,2]]
โ€บ Output: 17
๐Ÿ’ก Note: Connect A1 to B2 (cost 96) and A2 to B2 (cost 2), then connect B1 to A2 (cost 36) since B1 must be connected. Wait, let me recalculate: Connect A1 to B2 (cost 96) and A2 to both B1 (cost 36) and B2 (cost 2). Total cost = 96 + 36 + 2 = 134. Actually, the optimal is: A1โ†’B2 (96), A2โ†’B1 (36), but B2 needs connection, so A2โ†’B2 (2). Minimum cost is A1โ†’B2 (96) and A2โ†’B1 (36), but we need B2 connected, so add cheapest connection to B2 which is A2โ†’B2 (2). Wait, optimal is: A1โ†’B1 (15) and A2โ†’B2 (2) = 17.
example_2.py โ€” Multiple Connections
$ Input: cost = [[1,3,2],[4,1,1],[1,5,3]]
โ€บ Output: 4
๐Ÿ’ก Note: Connect A1โ†’B1 (1), A2โ†’B3 (1), A3โ†’B1 (1). Now B2 needs connection, cheapest is A2โ†’B2 (1). Total: 1+1+1+1=4.
example_3.py โ€” Edge Case
$ Input: cost = [[2,1],[1,4]]
โ€บ Output: 4
๐Ÿ’ก Note: Must connect all points. Connect A1โ†’B2 (1), A2โ†’B1 (1), but now need A1 connected to B1 (2). Total minimum cost is 4.

Constraints

  • size1 == cost.length
  • size2 == cost[i].length
  • 1 โ‰ค size2 โ‰ค size1 โ‰ค 12
  • size1 โ‰ฅ size2
  • 0 โ‰ค cost[i][j] โ‰ค 100

Visualization

Tap to expand
Building ABuilding BFloor A1Floor A2Floor A3Floor A4Floor B1Floor B2Bitmask States00: No B floors connected11: All B floors connected โœ“Dynamic Programming SolutionOptimal: O(n ร— 3^m) time complexity
Understanding the Visualization
1
Identify Groups
Building A (larger) has more floors than Building B (smaller)
2
Track Connections
Use bitmasks to efficiently track which floors in Building B are connected
3
Dynamic Planning
For each floor in Building A, determine optimal connections to Building B
4
Complete Network
Ensure all floors in both buildings have network connectivity
Key Takeaway
๐ŸŽฏ Key Insight: Use bitmasks to represent connection states of the smaller group, enabling efficient dynamic programming solution
Asked in
Google 42 Amazon 38 Microsoft 31 Meta 25
23.5K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen