Skip to content

Commit

Permalink
Create 20-minimum-cost-to-connect-two-groups-of-points.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 20, 2023
1 parent 9eafc05 commit 99458ba
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions 2023/06/20-minimum-cost-to-connect-two-groups-of-points.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
impl Solution {
pub fn connect_two_groups(cost: Vec<Vec<i32>>) -> i32 {
let mut dp = vec![vec![0x3f3f3f3f; 1 << cost[0].len()]; cost.len() + 1];
dp[0][0] = 0;
for i in 1..=cost.len() {
for s in 0..1 << cost[0].len() {
for k in 0..cost[0].len() {
if s & (1 << k) == 0 {
continue;
}
dp[i][s] = dp[i][s]
.min(dp[i][s ^ (1 << k)] + cost[i - 1][k])
.min(dp[i - 1][s] + cost[i - 1][k])
.min(dp[i - 1][s ^ (1 << k)] + cost[i - 1][k])
}
}
}
dp[cost.len()][(1 << cost[0].len()) - 1]
}
}

0 comments on commit 99458ba

Please sign in to comment.