We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My Disjoint Set solution. Just a small modification to a dictionnary for the size and parents instead of an array.
# ACUTAL SOLUTION WITH DISJOINT SET classDisjointSet:def__init__(self):self.parent={}self.size={}deffind(self,i):ifinotinself.parent:self.parent[i]=iself.size[i]=1returniifself.parent[i]!=i:self.parent[i]=self.find(self.parent[i])returnself.parent[i]defunionBySize(self,i,j):irep=self.find(i)jrep=self.find(j)ifirep==jrep:returnisize=self.size[irep]jsize=self.size[jrep]ifisize<jsize:self.parent[irep]=jrepself.size[jrep]+=self.size[irep]else:self.parent[jrep]=irepself.size[irep]+=self.size[jrep]defmaxCircle(queries):dset=DisjointSet()maxlen=0maxlens=[]forqueryinqueries:f1,f2=querydset.unionBySize(f1,f2)maxlen=max(maxlen,dset.size[dset.find(f1)])maxlens.append(maxlen)returnmaxlens
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Friend Circle Queries
You are viewing a single comment's thread. Return to all comments →
My Disjoint Set solution. Just a small modification to a dictionnary for the size and parents instead of an array.