zoukankan      html  css  js  c++  java
  • LeetCode 743. Network Delay Time

    原题链接在这里:https://leetcode.com/problems/network-delay-time/

    题目:

    There are N network nodes, labelled 1 to N.

    Given times, a list of travel times as directed edges times[i] = (u, v, w), where u is the source node, v is the target node, and w is the time it takes for a signal to travel from source to target.

    Now, we send a signal from a certain node K. How long will it take for all nodes to receive the signal? If it is impossible, return -1.

    Example 1:

    Input: times = [[2,1,1],[2,3,1],[3,4,1]], N = 4, K = 2
    Output: 2

    Note:

    1. N will be in the range [1, 100].
    2. K will be in the range [1, N].
    3. The length of times will be in the range [1, 6000].
    4. All edges times[i] = (u, v, w) will have 1 <= u, v <= N and 0 <= w <= 100.

    题解:

    Construct a graph. Then use the shortest time to traverse to the point.

    Have a minHeap based on the time spent to get to the point. With the graph and current point, find all the next points and calculate the time to get to that point, current.time + time from current to next point, put it to minHeap.

    Thus, we could always get to a point with shortest time.

    Note: Update visited set when poll out of minHeap but not before adding to the heap. Otherwise, if it takes very long time to next point and  add the next point to visited, later if there is shorter path to that point, it can't be added to minHeap.

    So update visited set after polling out of minHeap and update res if it is not visited before.

    Time Complexity: O(E+VlogV). It takes O(E) time to construct graph. O(VlogV) time to traverse all the points.

    Space: O(E+V). O(E) for graph, O(V) for minHeap and Set.

    AC Java:

     1 class Solution {
     2     public int networkDelayTime(int[][] times, int N, int K) {
     3         Map<Integer, List<int []>> graph = new HashMap<>();
     4         for(int [] edge : times){
     5             graph.putIfAbsent(edge[0], new ArrayList<int []>());
     6             graph.get(edge[0]).add(new int[]{edge[1], edge[2]});
     7         }
     8         
     9         int res = 0;
    10         
    11         PriorityQueue<int []> minHeap = new PriorityQueue<int []>((a,b) -> a[0]-b[0]);
    12         minHeap.add(new int[]{0, K});
    13         Set<Integer> visited = new HashSet<Integer>();
    14         int count = 0;
    15         
    16         while(!minHeap.isEmpty()){
    17             int[] cur = minHeap.poll();
    18             if(visited.contains(cur[1])){
    19                 continue;
    20             }
    21             
    22             visited.add(cur[1]);
    23             count++;
    24             res = cur[0];
    25             if(graph.containsKey(cur[1])){
    26                 for(int [] next : graph.get(cur[1])){
    27                     minHeap.add(new int[]{res+next[1], next[0]});
    28                 }
    29             }
    30         }
    31         
    32         return count == N ? res : -1;
    33     }
    34 }
  • 相关阅读:
    Java 8 stream的详细用法
    SpringBoot启动异常 Process finished with exit code 1
    GIT-版本管理-初阶使用
    升级 kubeadm 集群
    antdv 获取 axios文件上传实时进度
    Ant Design Vue 实现文件上传 (通过点击提交按钮后开始上传)
    Ant Design Vue 实现菜单栏根据url变化自动高亮和展开
    Do not access Object.prototype method 'hasOwnProperty' from target object
    Nginx配置WebSocket (包含nginx-ingress-controller)
    Django ORM 常用字段和参数/关系字段/ForeignKey操作/数据库查询优化(重要)/事务初识
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11162317.html
Copyright © 2011-2022 走看看