Applied AI Engineer (LLM) coding learning path Level 3 This level · 22 of 27

Network Delay Time

Medium Weighted graph Heap Shortest path

Problem

You have n nodes labeled from 1 to n, and directed edges times[i] = [u, v, w] meaning a signal takes w time to go from u to v. A signal starts at node k. Return the time when every node has received the signal, or -1 if it is impossible.

Original: LeetCode 743: Network Delay Time.

Examples

Example 1

Input`times = [[2, 1, 1], [2, 3, 1], [3, 4, 1]], n = 4, k = 2`
Output`2`

Example 2

Input`times = [[1, 2, 1]], n = 2, k = 1`
Output`1`

Example 3

Input`times = [[1, 2, 1]], n = 2, k = 2`
Output`-1`

Constraints

  • 1 <= k <= n <= 100
  • 1 <= times.length <= 6000
  • times[i].length == 3
  • 1 <= ui, vi <= n
  • ui != vi
  • 0 <= wi <= 100
  • All the pairs (ui, vi) are unique. (i.e., no multiple edges.)

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (2)
  1. LeetCode 743: Network Delay Timeleetcode.com
  2. Java SE 17 API: PriorityQueuedocs.oracle.com
Previous
Next