Skip to the code
cf.
+10 XP

#410 Notification Fan-Out Time

easy

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.

Example 1

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.

Example 2

Input: n = 3, connections = [[0,1]], start = 0
Output: -1

Person 2 has no contacts, so the announcement never reaches them.

Constraints

  • 1 <= n <= 10000
  • 0 <= connections.length <= 50000
  • 0 <= a, b, start < n
Saved in this browser

Run 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).