A docs site has n pages numbered 0 to n - 1. Each pair links[i] = [a, b] means page a has a link to page b. Links only work in that direction.
Write fewestClicks(n, links, from, to) that returns the fewest link clicks needed to get from page from to page to, or -1 if to can't be reached. If from === to, the answer is 0.
Input: n = 5, links = [[0,1],[1,2],[0,3],[3,2],[2,4]], from = 0, to = 4 Output: 3
One shortest route is 0 → 1 → 2 → 4: three clicks.
Input: n = 3, links = [[0,1],[2,1]], from = 0, to = 2 Output: -1
Page 0 links to 1, but nothing links on to page 2.
1 <= n <= 100000 <= links.length <= 500000 <= a, b, from, to < 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 #408 Fewest Clicks Between Pages. 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.