1595. Minimum Cost to Connect Two Groups of Points Hard

@problem@discussion
#Array#Dynamic Programming#Bit Manipulation#Matrix#Bitmask



1/**
2 * [1595] Minimum Cost to Connect Two Groups of Points
3 *
4 * You are given two groups of points where the first group has size1 points, the second group has size2 points, and size1 >= size2.
5 * 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.
6 * Return the minimum cost it takes to connect the two groups.
7 *  
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2020/09/03/ex1.jpg" style="width: 322px; height: 243px;" />
10 * Input: cost = [[15, 96], [36, 2]]
11 * Output: 17
12 * Explanation: The optimal way of connecting the groups is:
13 * 1--A
14 * 2--B
15 * This results in a total cost of 17.
16 * 
17 * Example 2:
18 * <img alt="" src="https://assets.leetcode.com/uploads/2020/09/03/ex2.jpg" style="width: 322px; height: 403px;" />
19 * Input: cost = [[1, 3, 5], [4, 1, 1], [1, 5, 3]]
20 * Output: 4
21 * Explanation: The optimal way of connecting the groups is:
22 * 1--A
23 * 2--B
24 * 2--C
25 * 3--A
26 * This results in a total cost of 4.
27 * Note that there are multiple points connected to point 2 in the first group and point A in the second group. This does not matter as there is no limit to the number of points that can be connected. We only care about the minimum total cost.
28 * 
29 * Example 3:
30 * 
31 * Input: cost = [[2, 5, 1], [3, 4, 7], [8, 1, 2], [6, 2, 4], [3, 8, 8]]
32 * Output: 10
33 * 
34 *  
35 * Constraints:
36 * 
37 * 	size1 == cost.length
38 * 	size2 == cost[i].length
39 * 	1 <= size1, size2 <= 12
40 * 	size1 >= size2
41 * 	0 <= cost[i][j] <= 100
42 * 
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/minimum-cost-to-connect-two-groups-of-points/
47// discuss: https://leetcode.com/problems/minimum-cost-to-connect-two-groups-of-points/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52    pub fn connect_two_groups(cost: Vec<Vec<i32>>) -> i32 {
53        0
54    }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_1595() {
65    }
66}
67


Back
© 2025 bowen.ge All Rights Reserved.