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 code with python. i dont know it fail in test case 7 how to debug
def roadsAndLibraries(n, c_lib, c_road, cities):
if c_road > c_lib:
return c_lib * n
list_cities = set(range(1, n + 1))
visited_cities = set()
city_groups = []
for city_pair in cities:
set_cities = set(city_pair)
visited_cities.update(set_cities)
merged = False
for i, group in enumerate(city_groups):
if group & set_cities:
city_groups[i] = group | set_cities
merged = True
break
if not merged:
city_groups.append(set_cities)
total_cost = 0
for group in city_groups:
total_cost += (len(group) - 1) * c_road + c_lib
unvisited_cities = list_cities.difference(visited_cities)
total_cost += len(unvisited_cities) * c_lib
return total_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 →
my code with python. i dont know it fail in test case 7 how to debug def roadsAndLibraries(n, c_lib, c_road, cities): if c_road > c_lib: return c_lib * n