diff --git a/DistributeWater.java b/DistributeWater.java new file mode 100644 index 0000000..6312e38 --- /dev/null +++ b/DistributeWater.java @@ -0,0 +1,57 @@ +// This solution assumes that the node to itself or well at a node is equivaluent to get pipe from another node which we can assume as an imaginary node -0 +// Then this solution will boil down to MST. This solution uses kruskals algorithm to find the minimum cost in connecting all nodes without cycles +// We first form all the edges including node 0 +// We keep forming union of all the nodes by picking the smallest possible edge so far +// We keep adding to min cost if both nodes or disjoint otherwise not +class Solution { + int minCost = 0; + int[] union; + public int minCostToSupplyWater(int n, int[] wells, int[][] pipes) { + int[][] edges = new int[pipes.length+wells.length][3]; + for(int i=0;i Integer.compare(a[2], b[2])); + + union = new int[n+1]; + for(int i=0;i