2097. Valid Arrangement of Pairs
UnknownView on LeetCode
Time: O(n)
Space: O(n)
Problem Overview
Valid Arrangement of Pairs (Unknown) asks you to solve a structured algorithmic task. This is a common Depth-First Search / Graph pattern in coding interviews. Eulerian path — find start node (outDegree - inDegree = 1 or arbitrary); Hierholzer's DFS.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Eulerian path — find start node (outDegree - inDegree = 1 or arbitrary); Hierholzer's DFS.
Related patterns: Depth-First Search, Graph, Eulerian Circuit
2097.cs
C#
// Approach: Eulerian path — find start node (outDegree - inDegree = 1 or arbitrary); Hierholzer's DFS.
// Time: O(n) Space: O(n)
public class Solution
{
public int[][] ValidArrangement(int[][] pairs)
{
List<int[]> ans = new List<int[]>();
Dictionary<int, Stack<int>> graph = new Dictionary<int, Stack<int>>();
Dictionary<int, int> outDegree = new Dictionary<int, int>();
Dictionary<int, int> inDegrees = new Dictionary<int, int>();
foreach (int[] pair in pairs)
{
int start = pair[0];
int end = pair[1];
if (!graph.ContainsKey(start))
graph[start] = new Stack<int>();
graph[start].Push(end);
outDegree[start] = outDegree.GetValueOrDefault(start, 0) + 1;
inDegrees[end] = inDegrees.GetValueOrDefault(end, 0) + 1;
}
int startNode = GetStartNode(graph, outDegree, inDegrees, pairs);
Euler(graph, startNode, ans);
ans.Reverse();
return ans.ToArray();
}
private int GetStartNode(Dictionary<int, Stack<int>> graph, Dictionary<int, int> outDegree,
Dictionary<int, int> inDegrees, int[][] pairs)
{
foreach (int u in graph.Keys)
{
if (outDegree.GetValueOrDefault(u, 0) - inDegrees.GetValueOrDefault(u, 0) == 1)
return u;
}
return pairs[0][0]; // Arbitrarily choose a node.
}
private void Euler(Dictionary<int, Stack<int>> graph, int u, List<int[]> ans)
{
Stack<int> stack = graph.GetValueOrDefault(u);
while (stack != null && stack.Count > 0)
{
int v = stack.Pop();
Euler(graph, v, ans);
ans.Add(new int[] { u, v });
}
}
}Was this solution helpful?
Related Problems
- 210. Course Schedule II(Medium)
- 310. Minimum Height Trees(Unknown)
- 684. Redundant Connection(Medium)
- 802. Find Eventual Safe States(Medium)
- 834. Sum of Distances in Tree(Unknown)
- 851. Loud and Rich(Medium)