Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Problem-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#Approach
# This uses to find the minimum spanning tree . Have the zero node where well at itself is connected
# find the union algorithm and if it ithe parents are diffrent parent then add to the nodes


#Complexities
#Time: O(m*n)log(M+n)
#Space: O(n)


from typing import List


class Solution:
def minCostToSupplyWater(self, n: int, wells: List[int], pipes: List[List[int]]) -> int:
self.parent = list(range(n+1))
self.rank = [1]*(n+1)
for i in range(len(wells)):
pipes.append([0,i+1,wells[i]])

pipes = sorted(pipes, key=lambda x: x[2])

cost =0
for houseA,houseB,pipelength in pipes:
if self.union(houseA,houseB):
cost+=pipelength
n-=1
if n==0:
return cost




def find(self,u):
parent = self.parent[u]
if parent != u:
self.parent[u]=self.find(parent)
return self.parent[u]

def union(self,u,v):
pu,pv = self.find(u),self.find(v)

if pu==pv:
return False

if self.rank[pu]>=self.rank[pv]:
self.rank[pu]+=1
self.parent[pv]=pu
elif self.rank[pv]>self.rank[pu]:
self.rank[pv]+=1
self.parent[pu]=pv
return True




s = Solution()
print(s.minCostToSupplyWater(3,[1,2,2],[[1,2,1],[2,3,1]]))
print(s.minCostToSupplyWater(2,[1,1],[[1,2,1],[1,2,2]]))



19 changes: 19 additions & 0 deletions Problem-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#Apporach
#check the pair of persons and return the knows and store the person who is popular left behind


#Complexities
#Time: O(N)
#Space: O(1)

class Solution:
def findCelebrity(self, n: int) -> int:
ans = 0
for i in range(1, n):
if knows(ans, i):
ans = i
for i in range(n):
if ans != i:
if knows(ans, i) or not knows(i, ans):
return -1
return ans