Code RoomWater supply network cost
HardPrep Room Coding #1081

Water supply network cost

CodingAlgorithms & data structuresSenior–Staff~35 min

You must supply water to n cities (1..n). For each city i you can either build a well in it at cost wells[i-1], or lay a bidirectional pipe between two cities given as [a, b, cost] to share water. A city has water if it has its own well or is connected by pipes to a city that has one. Return the minimum total cost to bring water to every city. wells has length n; there can be multiple pipes between the same pair.

Implement
min_water_cost(n: int, wells: list[int], pipes: list[list[int]]) → int
Examples
in[3,[1,2,2],[[1,2,1],[2,3,1]]]out3
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 35 min
InputExpectedGot
[3,[1,2,2],[[1,2,1],[2,3,1]]]3not run yetsample