|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +/** |
| 4 | + * 743. Network Delay Time |
| 5 | + * |
| 6 | + * There are N network nodes, labelled 1 to N. |
| 7 | + * |
| 8 | + * 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. |
| 9 | + * |
| 10 | + * 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. |
| 11 | + * |
| 12 | + * Note: |
| 13 | + * * N will be in the range [1, 100]. |
| 14 | + * * K will be in the range [1, N]. |
| 15 | + * * The length of times will be in the range [1, 6000]. |
| 16 | + * * All edges times[i] = (u, v, w) will have 1 <= u, v <= N and 0 <= w <= 100. |
| 17 | + */ |
| 18 | + |
| 19 | +public class _743 { |
| 20 | + public static class Solution1 { |
| 21 | + public static final int MAX = 100_00_00; |
| 22 | + |
| 23 | + private int[][] parseTimes(int[][] times, int N) { |
| 24 | + int[][] weights = new int[N + 1][N + 1]; |
| 25 | + |
| 26 | + for (int i = 0; i < N + 1; ++i) { |
| 27 | + for (int j = 0; j < N + 1; ++j) { |
| 28 | + weights[i][j] = MAX; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + for (int[] time : times) { |
| 33 | + int u = time[0]; |
| 34 | + int v = time[1]; |
| 35 | + int w = time[2]; |
| 36 | + weights[u][v] = w; |
| 37 | + } |
| 38 | + |
| 39 | + return weights; |
| 40 | + } |
| 41 | + |
| 42 | + private int findMinUnvisitedIndex(int[] a, boolean[] visited) { |
| 43 | + int min = Integer.MAX_VALUE; |
| 44 | + int minIdx = 1; |
| 45 | + |
| 46 | + for (int i = 1; i < a.length; ++i) { |
| 47 | + if (a[i] < min && !visited[i]) { |
| 48 | + min = a[i]; |
| 49 | + minIdx = i; |
| 50 | + } |
| 51 | + } |
| 52 | + return minIdx; |
| 53 | + } |
| 54 | + |
| 55 | + public int networkDelayTime(int[][] times, int N, int K) { |
| 56 | + // Time from u to v is weights[u][v] |
| 57 | + int[][] weights = parseTimes(times, N); |
| 58 | + |
| 59 | + // Time from node K to note i is totalTime[i] |
| 60 | + int[] totalTime = new int[N + 1]; |
| 61 | + for (int j = 0; j <= N; ++j) { |
| 62 | + totalTime[j] = MAX; |
| 63 | + } |
| 64 | + totalTime[K] = 0; |
| 65 | + |
| 66 | + boolean[] visited = new boolean[N + 1]; |
| 67 | + int visitCount = 0; |
| 68 | + |
| 69 | + while (visitCount != N) { |
| 70 | + int node = findMinUnvisitedIndex(totalTime, visited); |
| 71 | + |
| 72 | + // The smallest time to a node is more than our designated max |
| 73 | + // so it must be unreachable |
| 74 | + if (totalTime[node] >= MAX) { |
| 75 | + return -1; |
| 76 | + } |
| 77 | + |
| 78 | + visited[node] = true; |
| 79 | + visitCount += 1; |
| 80 | + |
| 81 | + // Update the time to each node if we try to visit it from our current node |
| 82 | + for (int i = 1; i <= N; ++i) { |
| 83 | + totalTime[i] = Math.min(weights[node][i] + totalTime[node], totalTime[i]); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // What is the maximum time to any particular node |
| 88 | + int maxVal = Integer.MIN_VALUE; |
| 89 | + for (int k = 1; k <= N; ++k) { |
| 90 | + maxVal = Math.max(totalTime[k], maxVal); |
| 91 | + } |
| 92 | + |
| 93 | + return maxVal; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments