Skip to content

Commit 2d6c5da

Browse files
solves network delay time in java
1 parent 761ec3d commit 2d6c5da

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@
379379
| 724 | [Find Pivot Index](https://leetcode.com/problems/find-pivot-index) | [![Java](assets/java.png)](src/FindPivotIndex.java) [![Python](assets/python.png)](python/find_pivot_index.py) | |
380380
| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers) | [![Java](assets/java.png)](src/SelfDividingNumbers.java) [![Python](assets/python.png)](python/self_dividing_number.py) | |
381381
| 733 | [Flood Fill](https://leetcode.com/problems/flood-fill) | [![Java](assets/java.png)](src/FloodFill.java) [![Python](assets/python.png)](python/flood_fill.py) | |
382+
| 743 | [Network Delay Time](https://leetcode.com/problems/network-delay-time) | [![Java](assets/java.png)](src/NetworkDelayTime.java) | |
382383
| 734 | [Sentence Similarity](https://leetcode.com/problems/sentence-similarity) | | |
383384
| 744 | [Find Smallest Letter Greater Than Target](https://leetcode.com/problems/find-smallest-letter-greater-than-target) | [![Java](assets/java.png)](src/FindSmallestLetterGreaterThanTarget.java) [![Python](assets/python.png)](python/find_smallest_letter_greater_than.py) | |
384385
| 746 | [Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs) | [![Java](assets/java.png)](src/MinCostClimbingStairs.java) [![Python](assets/python.png)](python/min_cost_climbing_stairs.py) | |

src/NetworkDelayTime.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// https://leetcode.com/problems/network-delay-time
2+
// Single source shortest path algorithm (Dijkstra's Algorithm)
3+
// T: O(E log(N) for dijkstra + N for searching max val)
4+
// S: O(E + N)
5+
6+
import java.util.Arrays;
7+
import java.util.Comparator;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
import java.util.PriorityQueue;
11+
import java.util.Queue;
12+
13+
public class NetworkDelayTime {
14+
private record Node(int value, long time) { }
15+
16+
public int networkDelayTime(int[][] times, int n, int k) {
17+
final Map<Integer, Map<Integer, Integer>> graph = createDirectedGraphFrom(times);
18+
final long[] minTimeTaken = getMinTimeTakenArray(n, k);
19+
final Queue<Node> queue = new PriorityQueue<Node>(Comparator.comparing(Node::time));
20+
queue.add(new Node(k, 0));
21+
22+
while (!queue.isEmpty()) {
23+
Node node = queue.poll();
24+
if (!graph.containsKey(node.value)) continue;
25+
Map<Integer, Integer> vertexNode = graph.get(node.value);
26+
for (int adjacent : vertexNode.keySet()) {
27+
if (minTimeTaken[node.value - 1] + vertexNode.get(adjacent) < minTimeTaken[adjacent - 1]) {
28+
minTimeTaken[adjacent - 1] = minTimeTaken[node.value - 1] + vertexNode.get(adjacent);
29+
queue.add(new Node(adjacent, minTimeTaken[adjacent - 1]));
30+
}
31+
}
32+
}
33+
34+
return allAreReached(minTimeTaken) ? (int) Arrays.stream(minTimeTaken).max().getAsLong() : -1;
35+
}
36+
37+
private Map<Integer, Map<Integer, Integer>> createDirectedGraphFrom(int[][] times) {
38+
final Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
39+
for (int[] time : times) {
40+
int start = time[0];
41+
int destination = time[1];
42+
int delay = time[2];
43+
44+
graph.putIfAbsent(start, new HashMap<>());
45+
graph.get(start).put(destination, delay);
46+
}
47+
return graph;
48+
}
49+
50+
private long[] getMinTimeTakenArray(int length, int start) {
51+
long[] array = new long[length];
52+
for (int i = 0 ; i < length ; i++) {
53+
array[i] = Integer.MAX_VALUE;
54+
}
55+
array[start - 1] = 0;
56+
return array;
57+
}
58+
59+
private boolean allAreReached(long[] array) {
60+
for (long val : array) {
61+
if (val == Integer.MAX_VALUE) return false;
62+
}
63+
return true;
64+
}
65+
}

0 commit comments

Comments
 (0)