A professional network shows "you are 3 connections away" on profiles. Users are numbered 0 to n - 1, and connections[i] = [a, b] means a and b are connected (both ways).
Write degreesApart(n, connections, a, b) that returns the fewest connections on a path from user a to user b, or -1 if there is no path. If a === b, return 0. The network is large, so try to search from both ends.
Input: n = 6, connections = [[0,1],[1,2],[2,3],[3,4],[0,5],[5,4]], a = 0, b = 3 Output: 3
Both 0-1-2-3 and 0-5-4-3 use 3 connections.
Input: n = 4, connections = [[0,1],[2,3]], a = 0, b = 3 Output: -1
Users 0 and 3 are in separate parts of the network.
1 <= n <= 1000000 <= connections.length <= 2000000 <= a, b < nRun tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #435 Degrees Between Two Users. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.