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.
defroadsAndLibraries(n,c_lib,c_road,cities):# Write your code hereifc_road>c_lib:returnn*c_libconns=defaultdict(set)foriinrange(n):conns[i+1]=set()foriinrange(len(cities)):conns[cities[i-1][0]].add(cities[i-1][1])conns[cities[i-1][1]].add(cities[i-1][0])conns=dict(sorted(conns.items(),key=lambdaitem:-len(item[1])))lens=[]forcinconns:lens.append(len(conns[c]))unvisited=set(range(1,n+1))cost=0whileunvisited:current=unvisited.pop()cost+=c_libconnected=set(conns[current])while(connected):nb=connected.pop()ifnbinunvisited:unvisited.remove(nb)foriinconns[nb]:connected.add(i)cost+=c_roadreturncost
co
return cost
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Roads and Libraries
You are viewing a single comment's thread. Return to all comments →
The shortest solution I was able to create: