"optimize water distribution in a village" Code Answer's
You're definitely familiar with the best coding language Whatever that developers use to develop their projects and they get all their queries like "optimize water distribution in a village" answered properly. Developers are finding an appropriate answer about optimize water distribution in a village related to the Whatever coding language. By visiting this online portal developers get answers concerning Whatever codes question like optimize water distribution in a village. Enter your desired code related query in the search bar and get every piece of information about Whatever code related question on optimize water distribution in a village.
optimize water distribution in a village
class Solution:
def minCostToSupplyWater(self, n: int, wells: List[int], pipes: List[List[int]]) -> int:
union_find = {i: i for i in range(n + 1)}
def find(x):
return x if x == union_find[x] else find(union_find[x])
def union(x, y):
px = find(x)
py = find(y)
union_find[px] = py
graph_wells = [[cost, 0, i] for i, cost in enumerate(wells, 1)]
graph_pipes = [[cost, i, j] for i, j, cost in pipes]
min_costs = 0
for cost, x, y in sorted(graph_wells + graph_pipes):
if find(x) == find(y):
continue
union(x, y)
min_costs += cost
n -= 1
if n == 0:
return min_costs
Source: github.com
All those coders who are working on the Whatever based application and are stuck on optimize water distribution in a village can get a collection of related answers to their query. Programmers need to enter their query on optimize water distribution in a village related to Whatever code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about optimize water distribution in a village for the programmers working on Whatever code while coding their module. Coders are also allowed to rectify already present answers of optimize water distribution in a village while working on the Whatever language code. Developers can add up suggestions if they deem fit any other answer relating to "optimize water distribution in a village". Visit this developer's friendly online web community, CodeProZone, and get your queries like optimize water distribution in a village resolved professionally and stay updated to the latest Whatever updates.