Skip to content

Add solution for 743 #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
96 changes: 96 additions & 0 deletions src/main/java/com/fishercoder/solutions/_743.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
28 changes: 28 additions & 0 deletions src/test/java/com/fishercoder/_743Test.java
Original file line number Diff line number Diff line change
@@ -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);
}
}