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.
This is what I have for swift, but I'm not passing 2 cases becase of performance, what am I doing wrong?
funcbfs(n:Int,m:Int,edges:[[Int]],s:Int)->[Int]{// Write your code herevarvisited=[Int:Int]()visited[s]=0varqueue=[s]vardictionary=[Int:[Int]]()foredgeinedges{dictionary[edge[0],default:[Int]()].append(edge[1])dictionary[edge[1],default:[Int]()].append(edge[0])}while!queue.isEmpty{letcurrentNode=queue.removeLast()guardletnodes=dictionary[currentNode]else{continue}fornodeinnodes{if(visited[node]==nil){visited[node]=visited[currentNode]!+6;queue.append(node)}elseif(visited[node]!>visited[currentNode]!+6){visited[node]=visited[currentNode]!+6;}}}varresult=[Int]()foriin1...n{ifi==s{continue}result.append(visited[i]??-1)}returnresult}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Breadth First Search: Shortest Reach
You are viewing a single comment's thread. Return to all comments →
This is what I have for swift, but I'm not passing 2 cases becase of performance, what am I doing wrong?