Skip to content

Commit cb7f392

Browse files
committed
Add solution #1192
1 parent af963bb commit cb7f392

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,175 LeetCode solutions in JavaScript
1+
# 1,176 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -924,6 +924,7 @@
924924
1189|[Maximum Number of Balloons](./solutions/1189-maximum-number-of-balloons.js)|Easy|
925925
1190|[Reverse Substrings Between Each Pair of Parentheses](./solutions/1190-reverse-substrings-between-each-pair-of-parentheses.js)|Medium|
926926
1191|[K-Concatenation Maximum Sum](./solutions/1191-k-concatenation-maximum-sum.js)|Medium|
927+
1192|[Critical Connections in a Network](./solutions/1192-critical-connections-in-a-network.js)|Hard|
927928
1200|[Minimum Absolute Difference](./solutions/1200-minimum-absolute-difference.js)|Easy|
928929
1206|[Design Skiplist](./solutions/1206-design-skiplist.js)|Hard|
929930
1207|[Unique Number of Occurrences](./solutions/1207-unique-number-of-occurrences.js)|Easy|
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* 1192. Critical Connections in a Network
3+
* https://leetcode.com/problems/critical-connections-in-a-network/
4+
* Difficulty: Hard
5+
*
6+
* There are n servers numbered from 0 to n - 1 connected by undirected server-to-server connections
7+
* forming a network where connections[i] = [ai, bi] represents a connection between servers ai and
8+
* bi. Any server can reach other servers directly or indirectly through the network.
9+
*
10+
* A critical connection is a connection that, if removed, will make some servers unable to reach
11+
* some other server.
12+
*
13+
* Return all critical connections in the network in any order.
14+
*/
15+
16+
/**
17+
* @param {number} n
18+
* @param {number[][]} connections
19+
* @return {number[][]}
20+
*/
21+
var criticalConnections = function(n, connections) {
22+
const graph = Array.from({ length: n }, () => []);
23+
const discoveryTimes = new Array(n).fill(-1);
24+
const lowestReachableTimes = new Array(n).fill(-1);
25+
const criticalEdges = [];
26+
let time = 0;
27+
28+
connections.forEach(([from, to]) => {
29+
graph[from].push(to);
30+
graph[to].push(from);
31+
});
32+
33+
exploreNode(0, -1);
34+
35+
return criticalEdges;
36+
37+
function exploreNode(current, parent) {
38+
discoveryTimes[current] = lowestReachableTimes[current] = time++;
39+
40+
for (const neighbor of graph[current]) {
41+
if (neighbor === parent) continue;
42+
if (discoveryTimes[neighbor] === -1) {
43+
exploreNode(neighbor, current);
44+
lowestReachableTimes[current] = Math.min(
45+
lowestReachableTimes[current],
46+
lowestReachableTimes[neighbor]
47+
);
48+
if (lowestReachableTimes[neighbor] > discoveryTimes[current]) {
49+
criticalEdges.push([current, neighbor]);
50+
}
51+
} else {
52+
lowestReachableTimes[current] = Math.min(
53+
lowestReachableTimes[current],
54+
discoveryTimes[neighbor]
55+
);
56+
}
57+
}
58+
}
59+
};

0 commit comments

Comments
 (0)