-
-
Notifications
You must be signed in to change notification settings - Fork 155
Minimum Cost to Connect Two Groups of Points Solution (Leetcode) Added #799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
173 changes: 173 additions & 0 deletions
173
...s/lc-solutions/1500-1599/1595 - Minimum Cost to Connect Two Groups of Points.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
--- | ||
id: Minimum Cost to Connect Two Groups of Points | ||
title: Minimum Cost to Connect Two Groups of Points | ||
sidebar_label: 1595 Minimum Cost to Connect Two Groups of Points | ||
tags: | ||
- Dynamic Programming | ||
- Bit Manipulation | ||
- Java | ||
- Bitmask | ||
- Matrix | ||
- Array | ||
description: "This document provides a solution where we return the minimum cost to connect the two groups." | ||
--- | ||
## 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 are given in an 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. | ||
|
||
### Examples | ||
|
||
**Example 1:** | ||
|
||
 | ||
|
||
**Input:** cost = [[15, 96], [36, 2]] | ||
|
||
**Output:** 17 | ||
|
||
**Explanation:** The optimal way of connecting the groups is: | ||
|
||
1--A | ||
|
||
2--B | ||
|
||
This results in a total cost of 17. | ||
|
||
**Example 2:** | ||
|
||
 | ||
|
||
**Input:** cost = [[1, 3, 5], [4, 1, 1], [1, 5, 3]] | ||
|
||
**Output:** 4 | ||
|
||
**Explanation:** The optimal way of connecting the groups is: | ||
|
||
1--A | ||
|
||
2--B | ||
|
||
2--C | ||
|
||
3--A | ||
|
||
This results in a total cost of 4. | ||
|
||
**Example 3:** | ||
|
||
**Input:** cost = [[2, 5, 1], [3, 4, 7], [8, 1, 2], [6, 2, 4], [3, 8, 8]] | ||
|
||
**Output:** 10 | ||
|
||
### Constraints | ||
|
||
- $size1 == cost.length$ | ||
- $size2 == cost[i].length$ | ||
- $1 <= size1,size2 <= 12$ | ||
- $size1 >= size2$ | ||
- $0 <= cost[i][j] <= 100$ | ||
|
||
--- | ||
## Approach | ||
There are four approaches discussed that helps to obtain the solution: | ||
|
||
1. **Define the Problem**: | ||
- We are given two groups of points with a cost matrix representing the connection cost between each pair of points from the two groups. | ||
|
||
- We need to ensure that every point in both groups is connected to at least one point in the other group. | ||
|
||
2. **Dynamic Programming with Memoization**: | ||
- Use dynamic programming (DP) with memoization to avoid redundant calculations. | ||
|
||
- Define a recursive function **'dfs(cost, i, mask, memo)'** where: | ||
- **i** is the current index in the first group. | ||
- Mask is a bitmask representing the subset of points in the second group that have been connected. | ||
- memo is a memoization table to store intermediate results. | ||
|
||
3. **Recursive Strategy**: | ||
- For each point in the first group, attempt to connect it to every point in the second group, updating the bitmask accordingly. | ||
|
||
- Once all points in the first group are processed, ensure that all points in the second group are connected by calculating the minimum additional cost for any remaining points. | ||
|
||
- Use memoization to store and reuse results of subproblems to improve efficiency. | ||
|
||
4. **Base and Transition**: | ||
- **Base Case:** If all points in the first group are processed (i == size1), calculate the cost to connect any remaining points in the second group that have not been connected. | ||
|
||
- **Transition:** For each point in the second group, update the bitmask and recursively calculate the minimum cost. | ||
|
||
## Solution for Minimum Cost to Connect Two Groups of Points | ||
|
||
This problem can be solved using dynamic programming. The problem requires connecting two groups of points such that each point in the first group is connected to at least one point in the second group and vice versa, with the goal of minimizing the total connection cost. This problem can be approached as a minimum cost bipartite matching problem with additional constraints. | ||
|
||
#### Code in Java | ||
|
||
```java | ||
import java.util.*; | ||
import java.util.Arrays; | ||
|
||
class Solution { | ||
public int connectTwoGroups(List<List<Integer>> cost) { | ||
int size1 = cost.size(); | ||
int size2 = cost.get(0).size(); | ||
|
||
// Memoization table | ||
int[][] memo = new int[size1][1 << size2]; | ||
for (int i = 0; i < size1; i++) { | ||
Arrays.fill(memo[i], -1); | ||
} | ||
|
||
return dfs(cost, 0, 0, memo); | ||
} | ||
|
||
private int dfs(List<List<Integer>> cost, int i, int mask, int[][] memo) { | ||
int size1 = cost.size(); | ||
int size2 = cost.get(0).size(); | ||
|
||
if (i == size1) { | ||
int remainingCost = 0; | ||
for (int j = 0; j < size2; j++) { | ||
if ((mask & (1 << j)) == 0) { | ||
int minCost = Integer.MAX_VALUE; | ||
for (int k = 0; k < size1; k++) { | ||
minCost = Math.min(minCost, cost.get(k).get(j)); | ||
} | ||
remainingCost += minCost; | ||
} | ||
} | ||
return remainingCost; | ||
} | ||
|
||
if (memo[i][mask] != -1) { | ||
return memo[i][mask]; | ||
} | ||
|
||
int minCost = Integer.MAX_VALUE; | ||
for (int j = 0; j < size2; j++) { | ||
minCost = Math.min(minCost, cost.get(i).get(j) + dfs(cost, i + 1, mask | (1 << j), memo)); | ||
} | ||
|
||
memo[i][mask] = minCost; | ||
return minCost; | ||
} | ||
} | ||
|
||
``` | ||
|
||
### Complexity Analysis | ||
|
||
#### Time Complexity: O(size1 × 2^size2 x size2) | ||
|
||
> **Reason**: There are 'size 1' points in the first group, and each point can potentially connect to any subset of the 'size2' points in the second group, leading to 2^size2 possible states for the bitmask. For each state, we iterate over all points in the second group, hence the additional factor of 'size2'. | ||
|
||
#### Space Complexity: O(size1 × 2^size2) | ||
|
||
> **Reason**: Additional space is used for the recursion stack, which is O(size1). | ||
|
||
# References | ||
|
||
- **LeetCode Problem:** [Minimum Cost to Connect Two Groups of Points](https://leetcode.com/problems/minimum-cost-to-connect-two-groups-of-points/description/) | ||
- **Solution Link:** [Minimum Cost to Connect Two Groups of Points Solution on LeetCode](https://leetcode.com/problems/minimum-cost-to-connect-two-groups-of-points/solutions/) | ||
- **Authors LeetCode Profile:** [Vivek Vardhan](https://leetcode.com/u/vivekvardhan43862/) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.