Skip to content

Commit 45b78b0

Browse files
havanagrawalfishercoder1534
authored andcommitted
Add solution for 743 (fishercoder1534#51)
1 parent 26589b6 commit 45b78b0

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ Your ideas/fixes/algorithms are more than welcome!
114114
|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|
115115
|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|
116116
|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|
117+
|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|
117118
|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|
118119
|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|
119120
|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|
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._743;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _743Test {
10+
private static _743.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _743.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
int[][] times = {{2, 1, 1}, {2, 3, 1}, {3, 4, 1}};
20+
assertEquals(solution1.networkDelayTime(times, 4, 2), 2);
21+
}
22+
23+
@Test
24+
public void test2() {
25+
int[][] times = {{2, 1, 1}, {2, 3, 1}, {3, 4, 1}};
26+
assertEquals(solution1.networkDelayTime(times, 4, 3), -1);
27+
}
28+
}

0 commit comments

Comments
 (0)