Skip to content

Commit 9446c8c

Browse files
committed
Add solution #1168
1 parent 65ef1ec commit 9446c8c

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,7 @@
10931093
1165|[Single-Row Keyboard](./solutions/1165-single-row-keyboard.js)|Easy|
10941094
1166|[Design File System](./solutions/1166-design-file-system.js)|Medium|
10951095
1167|[Minimum Cost to Connect Sticks](./solutions/1167-minimum-cost-to-connect-sticks.js)|Medium|
1096+
1168|[Optimize Water Distribution in a Village](./solutions/1168-optimize-water-distribution-in-a-village.js)|Hard|
10961097
1169|[Invalid Transactions](./solutions/1169-invalid-transactions.js)|Medium|
10971098
1170|[Compare Strings by Frequency of the Smallest Character](./solutions/1170-compare-strings-by-frequency-of-the-smallest-character.js)|Medium|
10981099
1171|[Remove Zero Sum Consecutive Nodes from Linked List](./solutions/1171-remove-zero-sum-consecutive-nodes-from-linked-list.js)|Medium|
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* 1168. Optimize Water Distribution in a Village
3+
* https://leetcode.com/problems/optimize-water-distribution-in-a-village/
4+
* Difficulty: Hard
5+
*
6+
* There are n houses in a village. We want to supply water for all the houses by building
7+
* wells and laying pipes.
8+
*
9+
* For each house i, we can either build a well inside it directly with cost wells[i - 1]
10+
* (note the -1 due to 0-indexing), or pipe in water from another well to it. The costs
11+
* to lay pipes between houses are given by the array pipes where each
12+
* pipes[j] = [house1j, house2j, costj] represents the cost to connect house1j and house2j
13+
* together using a pipe. Connections are bidirectional, and there could be multiple valid
14+
* connections between the same two houses with different costs.
15+
*
16+
* Return the minimum total cost to supply water to all houses.
17+
*/
18+
19+
/**
20+
* @param {number} n
21+
* @param {number[]} wells
22+
* @param {number[][]} pipes
23+
* @return {number}
24+
*/
25+
var minCostToSupplyWater = function(n, wells, pipes) {
26+
const edges = [];
27+
28+
for (let i = 0; i < n; i++) {
29+
edges.push([0, i + 1, wells[i]]);
30+
}
31+
32+
for (const [house1, house2, cost] of pipes) {
33+
edges.push([house1, house2, cost]);
34+
}
35+
36+
edges.sort((a, b) => a[2] - b[2]);
37+
38+
const parent = Array.from({ length: n + 1 }, (_, i) => i);
39+
let result = 0;
40+
let edgesUsed = 0;
41+
42+
for (const [from, to, cost] of edges) {
43+
if (union(from, to)) {
44+
result += cost;
45+
edgesUsed++;
46+
if (edgesUsed === n) break;
47+
}
48+
}
49+
50+
return result;
51+
52+
function find(x) {
53+
if (parent[x] !== x) {
54+
parent[x] = find(parent[x]);
55+
}
56+
return parent[x];
57+
}
58+
59+
function union(x, y) {
60+
const rootX = find(x);
61+
const rootY = find(y);
62+
if (rootX !== rootY) {
63+
parent[rootX] = rootY;
64+
return true;
65+
}
66+
return false;
67+
}
68+
};

0 commit comments

Comments
 (0)