Minimum Cost to Connect Two Groups of Points - Problem

You are given two groups of points where the first group has size1 points, the second group has size2 points, and size1 >= size2.

The cost of the connection between any two points is given in a size1 x size2 matrix where cost[i][j] is the cost of connecting point i of the first group and point j of the second group.

The groups are connected if each point in both groups is connected to one or more points in the opposite group. In other words, each point in the first group must be connected to at least one point in the second group, and each point in the second group must be connected to at least one point in the first group.

Return the minimum cost it takes to connect the two groups.

Input & Output

Example 1 — Basic Case
$ Input: cost = [[15,96],[36,2]]
Output: 17
💡 Note: Connect point 0 from group1 to point 0 from group2 (cost=15), and point 1 from group1 to point 1 from group2 (cost=2). Total cost = 15 + 2 = 17. All points are connected.
Example 2 — Multiple Connections
$ Input: cost = [[1,3,5],[4,1,1],[1,5,3]]
Output: 4
💡 Note: Connect point 0 to point 0 (cost=1), point 1 to point 1 (cost=1), point 2 to point 1 (cost=5). But we can do better: connect point 0 to point 0 (cost=1), point 1 to point 2 (cost=1), point 2 to point 1 (cost=5). However, the optimal is: point 0→0 (cost=1), point 1→1 (cost=1), point 2→2 (cost=3). But point 1 needs connection, so point 1→1 (cost=1), others connect optimally. Answer is 4.
Example 3 — Single Column
$ Input: cost = [[2],[3],[1]]
Output: 6
💡 Note: Only one point in group2. All three points in group1 must connect to it, so total cost = 2 + 3 + 1 = 6. The single group2 point is connected through all these connections.

Constraints

  • size1 == cost.length
  • size2 == cost[i].length
  • 1 ≤ size1, size2 ≤ 12
  • size1 ≥ size2
  • 0 ≤ cost[i][j] ≤ 100

Visualization

Tap to expand
Minimum Cost to Connect Two Groups INPUT Two Groups of Points Group 1 P0 P1 Group 2 Q0 Q1 15 96 36 2 Cost Matrix Q0 Q1 P0 15 96 P1 36 2 cost = [[15,96],[36,2]] size1=2, size2=2 ALGORITHM STEPS 1 Initialize DP Table dp[i][mask] = min cost for first i points, mask = connected G2 2 Bottom-Up Fill For each point in G1, try all G2 connections 3 Bitmask States mask tracks which G2 points are connected 4 Final Answer dp[size1][all_connected] mask = (1 << size2) - 1 DP Transitions P0-->Q0: cost=15, mask=01 P1-->Q1: cost=2, mask=11 Total: 15 + 2 = 17 Bitmask: 11 = both Q0,Q1 covered FINAL RESULT Optimal Connections P0 P1 Q0 Q1 15 2 Output 17 15 + 2 = 17 OK - All points connected! O(size1 * size2 * 2^size2) Key Insight: Use bitmask DP where mask represents which points in Group 2 are connected. For each point in Group 1, we must connect to at least one point in Group 2. After processing all Group 1 points, ensure all Group 2 points are covered (mask = all 1s). The optimal solution connects P0-->Q0 (cost 15) and P1-->Q1 (cost 2). TutorialsPoint - Minimum Cost to Connect Two Groups of Points | Optimized DP with Bottom-Up Approach
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
28.5K Views
Medium Frequency
~35 min Avg. Time
892 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