-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
57 lines (53 loc) · 1.48 KB
/
Solution.cs
File metadata and controls
57 lines (53 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class Solution
{
public int[][] ValidArrangement(int[][] pairs)
{
Dictionary<int, List<int>> graph = [];
Dictionary<int, int> inOutDegree = [];
for (int i = 0; i < pairs.Length; i++)
{
int u = pairs[i][0], v = pairs[i][1];
if (!graph.ContainsKey(u))
{
graph[u] = [];
}
graph[u].Add(v);
inOutDegree[u] = inOutDegree.GetValueOrDefault(u, 0) + 1;
inOutDegree[v] = inOutDegree.GetValueOrDefault(v, 0) - 1;
}
// Find starting node
int start = pairs[0][0];
foreach (var kvp in inOutDegree)
{
if (kvp.Value == 1)
{
start = kvp.Key;
break;
}
}
List<int> path = [];
Stack<int> stack = [];
stack.Push(start);
while (stack.Count > 0)
{
int curr = stack.Peek();
if (!graph.ContainsKey(curr) || graph[curr].Count == 0)
{
path.Add(stack.Pop());
}
else
{
var neighbor = graph[curr];
int next = neighbor[^1];
neighbor.RemoveAt(neighbor.Count - 1);
stack.Push(next);
}
}
List<int[]> result = [];
for (int i = path.Count - 1; i > 0; --i)
{
result.Add([path[i], path[i - 1]]);
}
return [.. result];
}
}