Skip to content

Commit b210956

Browse files
author
Partho Biswas
committed
1168. Optimize Water Distribution in a Village
1 parent f5ca6b5 commit b210956

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,8 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
428428
|19| **[332. Reconstruct Itinerary](https://tinyurl.com/gn2mv3k)** | [Python](https://tinyurl.com/wu6rdaw/332_Reconstruct_Itinerary.py)| [Vid 1](https://tinyurl.com/w47evd9), [Vid 2](https://tinyurl.com/uvop34p), [Art 1](https://tinyurl.com/tsy24pl), [Art 2](https://tinyurl.com/srazxo3), [Art 3](https://tinyurl.com/wluob5j), **[Art 4](https://tinyurl.com/y4s9crf6)** | Medium | **Important, Learned new things. Directed Graph. Eulerian path and top Sort** |
429429
|20| **[1153. String Transforms Into Another String](https://tinyurl.com/sv9hpo8)** | [Python](https://tinyurl.com/wu6rdaw/1153_String_Transforms_Into_Another_String.py)| **[Vid 1](https://tinyurl.com/ty2ga7d)**, [Vid 2](https://tinyurl.com/vxr9zxp), [Art 1](https://tinyurl.com/rklzgkk), [Art 2](https://tinyurl.com/seb6vqq), [Art 3](https://tinyurl.com/vfjwn5h), [Art 4](https://tinyurl.com/w62hpq2), [Art 5](https://tinyurl.com/tteucry), [Art 6](https://tinyurl.com/uvctnvz) | Extremely Hard | **TODO: Check again. Very Important and tricky, Learned new things. Digraph.** |
430430
|21| **[743. Network Delay Time](https://tinyurl.com/yd88jryl)** | [Python](https://tinyurl.com/wu6rdaw/743_Network_Delay_Time.py)| **[Official](https://tinyurl.com/rmcr2xk)**, **[Dijkstra 1](https://tinyurl.com/r8omw8l)**, **[Dijkstra 2](https://tinyurl.com/sscslz7)**, [Vid 1](https://tinyurl.com/vucexbc), **[Art 1](https://tinyurl.com/vbetlaq)**, [Art 2](https://tinyurl.com/yxdzrw3k), **[Art 3](https://tinyurl.com/whnbv4o)**, **[Art 4](https://tinyurl.com/roq3udj)** | Medium | **TODO: Check again. Very Important. Learned (Dijkstra, Bellman, Floyed). Check references section** |
431-
|22| [261. Graph Valid Tree](https://tinyurl.com/uw9ndt6) | [Python](https://tinyurl.com/wu6rdaw/261_Graph_Valid_Tree.py)| [Art 1](https://tinyurl.com/yx6ehrfu), **[Art 2](https://tinyurl.com/tlw6vda)**, **[Art 3](https://tinyurl.com/yhs85tj3)**, [Art 4](https://tinyurl.com/ydkfcc5x) | Medium | Important. BFS, DFS and [Union Find](https://tinyurl.com/yhs85tj3) |
431+
|22| **[261. Graph Valid Tree](https://tinyurl.com/uw9ndt6)** | [Python](https://tinyurl.com/wu6rdaw/261_Graph_Valid_Tree.py)| [Art 1](https://tinyurl.com/yx6ehrfu), **[Art 2](https://tinyurl.com/tlw6vda)**, **[Art 3](https://tinyurl.com/yhs85tj3)**, [Art 4](https://tinyurl.com/ydkfcc5x) | Medium | Important. BFS, DFS and [Union Find](https://tinyurl.com/yhs85tj3) |
432+
|23| **[1168. Optimize Water Distribution in a Village](https://tinyurl.com/ygh8sahd)** | [Python](https://tinyurl.com/wu6rdaw/1168_Optimize_Water_Distribution_in_a_Village.py)| **[Art 1](https://tinyurl.com/yjwwoxv3)**, **[Art 2](https://tinyurl.com/yhc3a7yr)**, **[Art 3](https://tinyurl.com/yf7fjz4v)**, [Art 4](https://tinyurl.com/yh7n9wps) | Hard | TODO: Check AGain. Prim's and Kruskal's algorithm. Important. |
432433

433434

434435
</p>
@@ -936,12 +937,14 @@ Learn the following modules by heart. Just knowing all of the following items wi
936937
<p>
937938

938939
01. [Graph Theory Playlist](https://tinyurl.com/rfgy88b)
939-
02. [Graph Theory Playlist 2](https://tinyurl.com/vgbgdl2)
940+
02. [Graph Theory Playlist 2](https://tinyurl.com/yfa57nc5)
940941
03. [Union Find \[Disjoint Set\] Playlist](https://tinyurl.com/srz7uua)
941942
04. [Disjoint Sets Data Structure - Weighted Union and Collapsing Find](https://www.youtube.com/watch?v=wU6udHRIkcc)
942943
05. [Codinginterviewclass.com](https://tinyurl.com/wpgl4nt)
943944
06. Single/All Source Shortest Path Algorithms: Dijkstra ([1](https://tinyurl.com/r8omw8l), [2](https://tinyurl.com/sscslz7)), Bellman-Ford([1](https://tinyurl.com/tghcsmz), [2](https://tinyurl.com/rdcax5b)), Floyd–Warshall([1](https://tinyurl.com/vsp4ulb), [2](https://tinyurl.com/s5wjb5a))
944945
07. Dijkstra’s Algorithm Vs Bellman-Ford Algorithm Vs Floyd Warshall Algorithm ([1](https://tinyurl.com/vxwc2je), [2](https://tinyurl.com/s6vff87), [3](https://tinyurl.com/s5o57nz)
946+
08. Minimum Spanning Tree Algorithm: Prim's ([1](https://tinyurl.com/vgbgdl2), [2](https://tinyurl.com/yecuqtty), Kruskal's ([1](https://tinyurl.com/vgbgdl2), [2](https://tinyurl.com/yhxpvpkn)
947+
09. Kruskal's algorithm Vs Prim's algorithm ([1](https://qr.ae/TSEPGc), [2](https://tinyurl.com/yz6399ck), [3](https://tinyurl.com/yz3q7vox)
945948

946949
</p>
947950
</details>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from collections import defaultdict
2+
from collections import deque
3+
import heapq
4+
5+
# Using Kruskal's algorithm
6+
class Solution(object):
7+
8+
def __init__(self):
9+
self.parents = list()
10+
11+
12+
13+
def find(self, node):
14+
if node != self.parents[node]:
15+
self.parents[node] = self.find(self.parents[node])
16+
return self.parents[node]
17+
18+
19+
20+
def union(self, node1, node2):
21+
parentOfNode1, parentOfNode2 = self.find(node1), self.find(node2)
22+
if parentOfNode1 != parentOfNode2:
23+
self.parents[parentOfNode1] = parentOfNode2
24+
return True
25+
return False
26+
27+
28+
29+
def minCostToSupplyWater(self, n, wells, pipes):
30+
"""
31+
:type n: int
32+
:type wells: List[int]
33+
:type pipes: List[List[int]]
34+
:rtype: int
35+
"""
36+
self.parents = list(range(n + 1))
37+
cost, undone = 0, n
38+
39+
totalPipes = []
40+
for i, w in enumerate(wells, 1):
41+
totalPipes.append([0,i,w]) # We can set 0 as water source that connected with each well which needs to be eventually unioned.
42+
43+
totalPipes = totalPipes + pipes
44+
totalPipes = sorted(totalPipes, key=lambda x: x[2])
45+
46+
for u, v, w in totalPipes:
47+
if self.union(u, v):
48+
cost += w
49+
undone -= 1
50+
if not undone:
51+
break
52+
return cost
53+
54+
55+
56+
57+
58+
59+
sol = Solution()
60+
n = 3
61+
wells = [1,2,2]
62+
pipes = [[1,2,1],[2,3,1]]
63+
out = sol.minCostToSupplyWater(n, wells, pipes)
64+
print("Res: ", out)
65+
66+
67+
68+

0 commit comments

Comments
 (0)