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.
letcnt=0;functionDfs(visited,tree,i){if(visited[i]){return;}visited[i]=true;++cntif(tree[i]){for(letvoftree[i]){Dfs(visited,tree,v);}}}/* * Complete the 'journeyToMoon' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: * 1. INTEGER n * 2. 2D_INTEGER_ARRAY astronaut */functionjourneyToMoon(n,astronaut){// Write your code herelettree={};for(const[a1,a2]ofastronaut){tree[a1]=tree[a1]??newSet();tree[a2]=tree[a2]??newSet();tree[a1].add(a2);tree[a2].add(a1);}leta=[];letans=0;for(leti=0;i<n;++i){if(!visited[i]){Dfs(visited,tree,i);a.push(cnt);cnt=0;}}for(leti=0;i<a.length;++i){for(letj=i+1;j<a.length;++j){ans+=a[i]*a[j];}}returnans;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Journey to the Moon
You are viewing a single comment's thread. Return to all comments →
Javascript