Practice/Amazon/Leetcode 1168. Optimize Water Distribution in a Village
CodingMust
A new residential development has n houses numbered from 1 to n. Each house needs access to water, which can be provided in two ways:
You are given:
n: the number of houseswells: an array where wells[i-1] is the cost to build a well at house ipipes: an array where each element [house1, house2, cost] represents the cost to lay a bidirectional pipe between house1 and house2Your task is to determine the minimum total cost to ensure every house has access to water.
Example 1:
` Input: n = 3, wells = [1, 2, 2], pipes = [[1, 2, 1], [2, 3, 1]] Output: 3 Explanation: We can supply water to all houses with minimum cost 3:
Example 2:
Input: n = 2, wells = [1, 1], pipes = [[1, 2, 10]] Output: 2 Explanation: Building individual wells at both houses costs 1 + 1 = 2, which is cheaper than building one well and using the expensive pipe (1 + 10 = 11).
Example 3:
Input: n = 5, wells = [5, 4, 3, 2, 1], pipes = [[1, 2, 1], [2, 3, 1], [3, 4, 1], [4, 5, 1]] Output: 5 Explanation: Build a well at house 5 (cheapest at cost 1), then connect all other houses through the pipe network (4 pipes × cost 1 = 4). Total: 5