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
The connection cost between any two points is given in a
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.
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
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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code