Leetcode 743: Network Delay Time

grid47
grid47
Exploring patterns and algorithms
Aug 24, 2024 7 min read

A network of nodes where the delay time is calculated, with the shortest delay glowing softly as it’s found.
Solution to LeetCode 743: Network Delay Time Problem

You are given a network of n nodes and a list of directed edges with travel times. You need to send a signal from a given node k. Return the minimum time it takes for all nodes to receive the signal, or return -1 if it is impossible.
Problem
Approach
Steps
Complexity
Input: The input consists of a list of directed edges `times[i] = (ui, vi, wi)` representing the travel time from node `ui` to node `vi`, as well as the number of nodes `n` and the starting node `k`.
Example: times = [[1, 2, 1], [1, 3, 2], [3, 4, 1]], n = 4, k = 1
Constraints:
• 1 <= k <= n <= 100
• 1 <= times.length <= 6000
• times[i].length == 3
• 1 <= ui, vi <= n
• ui != vi
• 0 <= wi <= 100
• All (ui, vi) pairs are unique.
Output: Return the minimum time it takes for all `n` nodes to receive the signal, or return `-1` if it is impossible for all nodes to receive the signal.
Example: For times = [[1, 2, 1], [1, 3, 2], [3, 4, 1]], n = 4, k = 1, the output is 3.
Constraints:
Goal: Find the minimum time it takes for all nodes to receive the signal or determine if it is impossible.
Steps:
• Use a graph representation to model the nodes and edges with the travel times.
• Use Dijkstra's algorithm to find the shortest path from the starting node `k` to all other nodes.
• If all nodes are reachable, return the maximum time taken by the farthest node.
• If any node is unreachable, return `-1`.
Goal: The input graph has up to 100 nodes and 6000 edges. The graph is directed, and there are no multiple edges between any two nodes.
Steps:
• 1 <= k <= n <= 100
• 1 <= times.length <= 6000
• 0 <= wi <= 100
Assumptions:
• The network will be represented as a directed graph with unique edges.
Input: For times = [[1, 2, 1], [1, 3, 2], [3, 4, 1]], n = 4, k = 1
Explanation: By using Dijkstra's algorithm, we can calculate the shortest time for the signal to reach each node, and we find that the signal reaches all nodes in 3 units of time.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus