From 509b6771d6046bd720c6dd73c0ac7a0604b84d8c Mon Sep 17 00:00:00 2001 From: Havan Date: Thu, 25 Apr 2019 13:03:08 -0700 Subject: [PATCH] Add solution for 743 --- README.md | 1 + .../java/com/fishercoder/solutions/_743.java | 96 +++++++++++++++++++ src/test/java/com/fishercoder/_743Test.java | 28 ++++++ 3 files changed, 125 insertions(+) create mode 100644 src/main/java/com/fishercoder/solutions/_743.java create mode 100644 src/test/java/com/fishercoder/_743Test.java diff --git a/README.md b/README.md index a5f69a8fd0..12b1423b7a 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ Your ideas/fixes/algorithms are more than welcome! |747|[Largest Number Greater Than Twice of Others](https://leetcode.com/problems/largest-number-greater-than-twice-of-others/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_747.java) | O(n) | O(1) | |Easy| |746|[Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_746.java) | O(n) | O(1) | |Easy| |744|[Find Smallest Letter Greater Than Target](https://leetcode.com/problems/find-smallest-letter-greater-than-target/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_744.java) | O(logn) | O(1) || Easy| +|743|[Network Delay Time](https://leetcode.com/problems/network-delay-time/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_743.java) | O(n^2 + e) | O(n^2) || Medium|Graph, Djikstra| |740|[Delete and Earn](https://leetcode.com/problems/delete-and-earn/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_740.java) | O(n) | O(n) | |Medium| |739|[Daily Temperatures](https://leetcode.com/problems/daily-temperatures/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_739.java) | O(n^2) | O(1) | |Medium| |738|[Monotone Increasing Digits](https://leetcode.com/problems/monotone-increasing-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_738.java) | O(n) | O(1) | |Medium| diff --git a/src/main/java/com/fishercoder/solutions/_743.java b/src/main/java/com/fishercoder/solutions/_743.java new file mode 100644 index 0000000000..46bc1f43b3 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/_743.java @@ -0,0 +1,96 @@ +package com.fishercoder.solutions; + +/** + * 743. 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. + * + * Note: + * * N will be in the range [1, 100]. + * * K will be in the range [1, N]. + * * The length of times will be in the range [1, 6000]. + * * All edges times[i] = (u, v, w) will have 1 <= u, v <= N and 0 <= w <= 100. + */ + +public class _743 { + public static class Solution1 { + public static final int MAX = 100_00_00; + + private int[][] parseTimes(int[][] times, int N) { + int[][] weights = new int[N + 1][N + 1]; + + for (int i = 0; i < N + 1; ++i) { + for (int j = 0; j < N + 1; ++j) { + weights[i][j] = MAX; + } + } + + for (int[] time : times) { + int u = time[0]; + int v = time[1]; + int w = time[2]; + weights[u][v] = w; + } + + return weights; + } + + private int findMinUnvisitedIndex(int[] a, boolean[] visited) { + int min = Integer.MAX_VALUE; + int minIdx = 1; + + for (int i = 1; i < a.length; ++i) { + if (a[i] < min && !visited[i]) { + min = a[i]; + minIdx = i; + } + } + return minIdx; + } + + public int networkDelayTime(int[][] times, int N, int K) { + // Time from u to v is weights[u][v] + int[][] weights = parseTimes(times, N); + + // Time from node K to note i is totalTime[i] + int[] totalTime = new int[N + 1]; + for (int j = 0; j <= N; ++j) { + totalTime[j] = MAX; + } + totalTime[K] = 0; + + boolean[] visited = new boolean[N + 1]; + int visitCount = 0; + + while (visitCount != N) { + int node = findMinUnvisitedIndex(totalTime, visited); + + // The smallest time to a node is more than our designated max + // so it must be unreachable + if (totalTime[node] >= MAX) { + return -1; + } + + visited[node] = true; + visitCount += 1; + + // Update the time to each node if we try to visit it from our current node + for (int i = 1; i <= N; ++i) { + totalTime[i] = Math.min(weights[node][i] + totalTime[node], totalTime[i]); + } + } + + // What is the maximum time to any particular node + int maxVal = Integer.MIN_VALUE; + for (int k = 1; k <= N; ++k) { + maxVal = Math.max(totalTime[k], maxVal); + } + + return maxVal; + } + } +} diff --git a/src/test/java/com/fishercoder/_743Test.java b/src/test/java/com/fishercoder/_743Test.java new file mode 100644 index 0000000000..825ea25425 --- /dev/null +++ b/src/test/java/com/fishercoder/_743Test.java @@ -0,0 +1,28 @@ +package com.fishercoder; + +import com.fishercoder.solutions._743; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class _743Test { + private static _743.Solution1 solution1; + + @BeforeClass + public static void setup() { + solution1 = new _743.Solution1(); + } + + @Test + public void test1() { + int[][] times = {{2, 1, 1}, {2, 3, 1}, {3, 4, 1}}; + assertEquals(solution1.networkDelayTime(times, 4, 2), 2); + } + + @Test + public void test2() { + int[][] times = {{2, 1, 1}, {2, 3, 1}, {3, 4, 1}}; + assertEquals(solution1.networkDelayTime(times, 4, 3), -1); + } +}