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.
functionroadsAndLibraries(n,c_lib,c_road,cities){// Write your code hereif(c_road>=c_lib)returnn*c_libletarr=[]for(leti=1;i<=n;i++)arr[i]=[i]for(leti=0;i<cities.length;i++){let[m,n]=cities[i]if(arr[m]===arr[n])continueif(arr[m].length<arr[n].length)[m,n]=[n,m]for(lettempofarr[n])arr[m].push(temp),arr[temp]=arr[m]}letgroups=newSet()for(lettempofarr.slice(1))groups.add(temp)letcost=groups.size*c_lib+Array.from(groups).map(group=>group.length-1).reduce((acc,cur)=>acc+cur*c_road,0)returncost}
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 →
JavaScript