Skip to content

Commit 66e9224

Browse files
committed
1 parent 06e42b1 commit 66e9224

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1203,3 +1203,4 @@ mod s1591_strange_printer_ii;
12031203
mod s1592_rearrange_spaces_between_words;
12041204
mod s1593_split_a_string_into_the_max_number_of_unique_substrings;
12051205
mod s1594_maximum_non_negative_product_in_a_matrix;
1206+
mod s1595_minimum_cost_to_connect_two_groups_of_points;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
*/
44+
pub 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+
51+
impl Solution {
52+
// Credit: https://leetcode.com/problems/minimum-cost-to-connect-two-groups-of-points/solutions/3175220/just-a-runnable-solution/
53+
pub fn connect_two_groups(cost: Vec<Vec<i32>>) -> i32 {
54+
let cost: Vec<Vec<_>> = cost
55+
.iter()
56+
.map(|v| v.iter().map(|&x| x as i64).collect())
57+
.collect();
58+
59+
let (sz1, sz2) = (cost.len(), cost[0].len());
60+
let mut dp = vec![vec![1_000_000_007; 1 << sz2]; sz1 + 1];
61+
62+
dp[sz1][(1 << sz2) - 1] = 0;
63+
64+
for i in (0..sz1).rev() {
65+
for mask in (0..(1 << sz2)).rev() {
66+
for j in 0..sz2 {
67+
dp[i][mask] = dp[i][mask].min(cost[i][j] + dp[i + 1][mask | (1 << j)]);
68+
if mask & (1 << j) == 0 {
69+
dp[i][mask] = dp[i][mask].min(cost[i][j] + dp[i][mask | (1 << j)]);
70+
}
71+
}
72+
}
73+
}
74+
75+
dp[0][0] as i32
76+
}
77+
}
78+
79+
// submission codes end
80+
81+
#[cfg(test)]
82+
mod tests {
83+
use super::*;
84+
85+
#[test]
86+
fn test_1595_example_1() {
87+
let cost = vec![vec![15, 96], vec![36, 2]];
88+
89+
let result = 17;
90+
91+
assert_eq!(Solution::connect_two_groups(cost), result);
92+
}
93+
94+
#[test]
95+
fn test_1595_example_2() {
96+
let cost = vec![vec![1, 3, 5], vec![4, 1, 1], vec![1, 5, 3]];
97+
98+
let result = 4;
99+
100+
assert_eq!(Solution::connect_two_groups(cost), result);
101+
}
102+
103+
#[test]
104+
fn test_1595_example_3() {
105+
let cost = vec![
106+
vec![2, 5, 1],
107+
vec![3, 4, 7],
108+
vec![8, 1, 2],
109+
vec![6, 2, 4],
110+
vec![3, 8, 8],
111+
];
112+
113+
let result = 10;
114+
115+
assert_eq!(Solution::connect_two_groups(cost), result);
116+
}
117+
}

0 commit comments

Comments
 (0)