A chat app relays an announcement from person to person. People are numbered 0 to n - 1, and connections[i] = [a, b] means a and b are contacts (the link works both ways). At second 0, only person start has the announcement. Every second, everyone who has it passes it to all of their contacts.
Write fanOutTime(n, connections, start) that returns the number of seconds until everyone has the announcement, or -1 if some people never get it.
Input: n = 5, connections = [[0,1],[1,2],[2,3],[1,4]], start = 1 Output: 2
At second 1, people 0, 2, and 4 have it. At second 2, person 3 gets it from 2.
Input: n = 3, connections = [[0,1]], start = 0 Output: -1
Person 2 has no contacts, so the announcement never reaches them.
1 <= n <= 100000 <= connections.length <= 500000 <= a, b, start < 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 #410 Notification Fan-Out Time. 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.