< Summary

Information
Class: LeetCode.Algorithms.ValidArrangementOfPairs.ValidArrangementOfPairsDepthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ValidArrangementOfPairs\ValidArrangementOfPairsDepthFirstSearch.cs
Line coverage
97%
Covered lines: 48
Uncovered lines: 1
Coverable lines: 49
Total lines: 99
Line coverage: 97.9%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ValidArrangement(...)100%44100%
FindStartNode(...)75%4490.9%
BuildEulerianPath(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ValidArrangementOfPairs\ValidArrangementOfPairsDepthFirstSearch.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.ValidArrangementOfPairs;
 13
 14/// <inheritdoc />
 15public sealed class ValidArrangementOfPairsDepthFirstSearch : IValidArrangementOfPairs
 16{
 17    /// <summary>
 18    ///     Time complexity - O(E + V), where E is the number of edges (pairs) and V is the number of vertices (unique n
 19    ///     Space complexity - O(E + V), where E is the number of edges (pairs) and V is the number of vertices (unique 
 20    /// </summary>
 21    /// <param name="pairs"></param>
 22    /// <returns></returns>
 23    public int[][] ValidArrangement(int[][] pairs)
 124    {
 125        var startToEndsDictionary = new Dictionary<int, Stack<int>>();
 126        var endToCountDictionary = new Dictionary<int, int>();
 127        var startToCountDictionary = new Dictionary<int, int>();
 28
 129        var pairsLength = pairs.Length;
 30
 2631        for (var i = 0; i < pairsLength; i++)
 1232        {
 1233            var pair = pairs[i];
 1234            var start = pair[0];
 1235            var end = pair[1];
 36
 1237            if (!startToEndsDictionary.TryGetValue(start, out var ends))
 1038            {
 1039                ends = new Stack<int>();
 40
 1041                startToEndsDictionary[start] = ends;
 1042            }
 43
 1244            ends.Push(end);
 45
 1246            startToCountDictionary[start] = startToCountDictionary.GetValueOrDefault(start) + 1;
 1247            endToCountDictionary[end] = endToCountDictionary.GetValueOrDefault(end) + 1;
 1248        }
 49
 150        var startNode = FindStartNode(startToCountDictionary, endToCountDictionary, pairs);
 51
 152        var pairsIndex = pairsLength - 1;
 53
 154        BuildEulerianPath(startToEndsDictionary, pairs, startNode, ref pairsIndex);
 55
 156        return pairs;
 157    }
 58
 59    private static int FindStartNode(Dictionary<int, int> startToCountDictionary,
 60        Dictionary<int, int> endToCountDictionary, int[][] pairs)
 161    {
 162        var startNode = pairs[0][0];
 63
 664        foreach (var (start, startCount) in startToCountDictionary)
 265        {
 266            var endCount = endToCountDictionary.GetValueOrDefault(start);
 67
 268            if (startCount > endCount)
 169            {
 170                return start;
 71            }
 172        }
 73
 074        return startNode;
 175    }
 76
 77    private static void BuildEulerianPath(Dictionary<int, Stack<int>> startToEndsDictionary, int[][] pairs, int start,
 78        ref int pairsIndex)
 1379    {
 1380        if (!startToEndsDictionary.TryGetValue(start, out var ends))
 181        {
 182            return;
 83        }
 84
 2485        while (ends.Count > 0)
 1286        {
 1287            var end = ends.Pop();
 88
 1289            BuildEulerianPath(startToEndsDictionary, pairs, end, ref pairsIndex);
 90
 1291            var pair = pairs[pairsIndex];
 92
 1293            pair[0] = start;
 1294            pair[1] = end;
 95
 1296            pairsIndex--;
 1297        }
 1398    }
 99}