LeetCode 743: Network Delay Time Solution

Master LeetCode problem 743 (Network Delay Time), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

Problem Explanation

Explanation

To solve this problem, we can use Dijkstra's algorithm starting from the source node k. We will maintain a distance array to keep track of the minimum time it takes to reach each node. The algorithm involves relaxing edges and updating the distance array until all nodes are visited or we can't reach some nodes.

  1. Initialize all distances to infinity except the source node k to 0.
  2. Initialize a priority queue to store nodes based on their distances.
  3. While the priority queue is not empty:
    • Pop the node with the minimum distance.
    • Relax all outgoing edges from this node, updating the distance array if a shorter path is found.
  4. After processing all nodes, return the maximum value in the distance array if all nodes are reachable. Otherwise, return -1.

Time complexity: The time complexity of Dijkstra's algorithm is O((V + E) log V), where V is the number of nodes and E is the number of edges.

Space complexity: The space complexity is O(V) for storing the distance array and the priority queue.

Solution Code

class Solution {
    public int networkDelayTime(int[][] times, int n, int k) {
        int[] dist = new int[n + 1];
        Arrays.fill(dist, Integer.MAX_VALUE);
        dist[k] = 0;
        
        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[1] - b[1]);
        pq.offer(new int[]{k, 0});
        
        Map<Integer, List<int[]>> graph = new HashMap<>();
        for (int[] time : times) {
            graph.computeIfAbsent(time[0], x -> new ArrayList<>()).add(new int[]{time[1], time[2]});
        }
        
        while (!pq.isEmpty()) {
            int[] cur = pq.poll();
            int node = cur[0];
            int time = cur[1];
            
            if (!graph.containsKey(node)) continue;
            
            for (int[] next : graph.get(node)) {
                int neighbor = next[0];
                int newTime = time + next[1];
                
                if (newTime < dist[neighbor]) {
                    dist[neighbor] = newTime;
                    pq.offer(new int[]{neighbor, newTime});
                }
            }
        }
        
        int maxTime = Arrays.stream(dist).max().getAsInt();
        return maxTime == Integer.MAX_VALUE ? -1 : maxTime;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 743 (Network Delay Time)?

This page provides optimized solutions for LeetCode problem 743 (Network Delay Time) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 743 (Network Delay Time)?

The time complexity for LeetCode 743 (Network Delay Time) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 743 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 743 in Java, C++, or Python.

Back to LeetCode Solutions