Skip to content

Commit 0b7fef9

Browse files
committed
Add solution #1595
1 parent 561e559 commit 0b7fef9

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,408 LeetCode solutions in JavaScript
1+
# 1,409 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1232,6 +1232,7 @@
12321232
1592|[Rearrange Spaces Between Words](./solutions/1592-rearrange-spaces-between-words.js)|Easy|
12331233
1593|[Split a String Into the Max Number of Unique Substrings](./solutions/1593-split-a-string-into-the-max-number-of-unique-substrings.js)|Medium|
12341234
1594|[Maximum Non Negative Product in a Matrix](./solutions/1594-maximum-non-negative-product-in-a-matrix.js)|Medium|
1235+
1595|[Minimum Cost to Connect Two Groups of Points](./solutions/1595-minimum-cost-to-connect-two-groups-of-points.js)|Hard|
12351236
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
12361237
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12371238
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* 1595. Minimum Cost to Connect Two Groups of Points
3+
* https://leetcode.com/problems/minimum-cost-to-connect-two-groups-of-points/
4+
* Difficulty: Hard
5+
*
6+
* You are given two groups of points where the first group has size1 points, the second group
7+
* has size2 points, and size1 >= size2.
8+
*
9+
* The cost of the connection between any two points are given in an size1 x size2 matrix where
10+
* cost[i][j] is the cost of connecting point i of the first group and point j of the second group.
11+
* The groups are connected if each point in both groups is connected to one or more points in the
12+
* opposite group. In other words, each point in the first group must be connected to at least one
13+
* point in the second group, and each point in the second group must be connected to at least one
14+
* point in the first group.
15+
*
16+
* Return the minimum cost it takes to connect the two groups.
17+
*/
18+
19+
/**
20+
* @param {number[][]} cost
21+
* @return {number}
22+
*/
23+
var connectTwoGroups = function(cost) {
24+
const size1 = cost.length;
25+
const size2 = cost[0].length;
26+
27+
const minCostGroup2 = new Array(size2).fill(Infinity);
28+
for (let j = 0; j < size2; j++) {
29+
for (let i = 0; i < size1; i++) {
30+
minCostGroup2[j] = Math.min(minCostGroup2[j], cost[i][j]);
31+
}
32+
}
33+
34+
const memo = new Array(size1).fill(0).map(() => new Array(1 << size2).fill(-1));
35+
36+
return dfs(0, 0);
37+
38+
function dfs(i, mask) {
39+
if (i === size1) {
40+
let remainingCost = 0;
41+
for (let j = 0; j < size2; j++) {
42+
if ((mask & (1 << j)) === 0) {
43+
remainingCost += minCostGroup2[j];
44+
}
45+
}
46+
return remainingCost;
47+
}
48+
49+
if (memo[i][mask] !== -1) return memo[i][mask];
50+
51+
let minCost = Infinity;
52+
53+
for (let j = 0; j < size2; j++) {
54+
minCost = Math.min(
55+
minCost,
56+
cost[i][j] + dfs(i + 1, mask | (1 << j))
57+
);
58+
}
59+
60+
memo[i][mask] = minCost;
61+
return minCost;
62+
}
63+
};

0 commit comments

Comments
 (0)