Solving Leetcode Interviews in Seconds with AI: Critical Connections in a Network
Introduction
In this blog post, we will explore how to solve the LeetCode problem "1192" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
There are n servers numbered from 0 to n - 1 connected by undirected server-to-server connections forming a network where connections[i] = [ai, bi] represents a connection between servers ai and bi. Any server can reach other servers directly or indirectly through the network. A critical connection is a connection that, if removed, will make some servers unable to reach some other server. Return all critical connections in the network in any order. Example 1: Input: n = 4, connections = [[0,1],[1,2],[2,0],[1,3]] Output: [[1,3]] Explanation: [[3,1]] is also accepted. Example 2: Input: n = 2, connections = [[0,1]] Output: [[0,1]] Constraints: 2 <= n <= 105 n - 1 <= connections.length <= 105 0 <= ai, bi <= n - 1 ai != bi There are no repeated connections.
Explanation
Here's a breakdown of the solution:
Tarjan's Algorithm: The core idea is to use Tarjan's algorithm to find bridges (critical connections) in the graph. Tarjan's algorithm is an efficient way to find strongly connected components and articulation points/bridges in a graph.
Depth-First Search (DFS): A DFS is performed, keeping track of the discovery time (
disc) and lowest reachable ancestor (low) for each node. Bridges are identified whendisc[u] < low[v]for an edge(u, v). This meansvcannot reachuor any ancestor ofuwithout going through the edge(u, v), hence it's a critical connection.Efficient Implementation: The graph is represented using an adjacency list for fast neighbor lookups.
discandlowarrays are used to efficiently track discovery times and lowest reachable ancestors during the DFS.Time & Space Complexity: O(V+E) for both time and space, where V is the number of vertices (servers) and E is the number of edges (connections).
Code
def criticalConnections(n: int, connections: list[list[int]]) -> list[list[int]]:
"""
Finds all critical connections (bridges) in a network of servers.
Args:
n: The number of servers.
connections: A list of connections between servers.
Returns:
A list of critical connections.
"""
graph = [[] for _ in range(n)]
for u, v in connections:
graph[u].append(v)
graph[v].append(u)
disc = [-1] * n # Discovery time of each node
low = [-1] * n # Lowest reachable ancestor of each node
parent = [-1] * n # Parent of each node in DFS tree
critical_connections = []
time = 0
def dfs(u):
nonlocal time
disc[u] = time
low[u] = time
time += 1
for v in graph[u]:
if disc[v] == -1: # If v is not visited
parent[v] = u
dfs(v)
low[u] = min(low[u], low[v])
if disc[u] < low[v]:
critical_connections.append([u, v])
elif v != parent[u]: # Ignore the parent edge
low[u] = min(low[u], disc[v])
for i in range(n):
if disc[i] == -1:
dfs(i)
return critical_connections