Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

154. Network Delay Time

MediumDay 45 of 60Graphs

A network has n nodes labelled 1 to n. times[i] = [u, v, w] means a signal sent from u reaches v after w units of time (edges are directed). A signal is sent from node k. Return the time it takes for all nodes to receive it, or -1 if some node can never receive it.

Example 1
Input: times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
Output: 2
Nodes 1 and 3 get it at time 1, node 4 at time 2.
Example 2
Input: times = [[1,2,1]], n = 2, k = 2
Output: -1
There is no edge out of node 2, so node 1 never hears it.
Constraints
  • 1 <= k <= n <= 100
  • 1 <= times.length <= 6000
  • 1 <= u, v <= n, u != v
  • 0 <= w <= 100
  • All (u, v) pairs are unique
Hints
Frequently asked at: Amazon, Google, Uber
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min