| | | 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 | | |
| | | 12 | | namespace LeetCode.Algorithms.ValidArrangementOfPairs; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public 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) |
| | 1 | 24 | | { |
| | 1 | 25 | | var startToEndsDictionary = new Dictionary<int, Stack<int>>(); |
| | 1 | 26 | | var endToCountDictionary = new Dictionary<int, int>(); |
| | 1 | 27 | | var startToCountDictionary = new Dictionary<int, int>(); |
| | | 28 | | |
| | 1 | 29 | | var pairsLength = pairs.Length; |
| | | 30 | | |
| | 26 | 31 | | for (var i = 0; i < pairsLength; i++) |
| | 12 | 32 | | { |
| | 12 | 33 | | var pair = pairs[i]; |
| | 12 | 34 | | var start = pair[0]; |
| | 12 | 35 | | var end = pair[1]; |
| | | 36 | | |
| | 12 | 37 | | if (!startToEndsDictionary.TryGetValue(start, out var ends)) |
| | 10 | 38 | | { |
| | 10 | 39 | | ends = new Stack<int>(); |
| | | 40 | | |
| | 10 | 41 | | startToEndsDictionary[start] = ends; |
| | 10 | 42 | | } |
| | | 43 | | |
| | 12 | 44 | | ends.Push(end); |
| | | 45 | | |
| | 12 | 46 | | startToCountDictionary[start] = startToCountDictionary.GetValueOrDefault(start) + 1; |
| | 12 | 47 | | endToCountDictionary[end] = endToCountDictionary.GetValueOrDefault(end) + 1; |
| | 12 | 48 | | } |
| | | 49 | | |
| | 1 | 50 | | var startNode = FindStartNode(startToCountDictionary, endToCountDictionary, pairs); |
| | | 51 | | |
| | 1 | 52 | | var pairsIndex = pairsLength - 1; |
| | | 53 | | |
| | 1 | 54 | | BuildEulerianPath(startToEndsDictionary, pairs, startNode, ref pairsIndex); |
| | | 55 | | |
| | 1 | 56 | | return pairs; |
| | 1 | 57 | | } |
| | | 58 | | |
| | | 59 | | private static int FindStartNode(Dictionary<int, int> startToCountDictionary, |
| | | 60 | | Dictionary<int, int> endToCountDictionary, int[][] pairs) |
| | 1 | 61 | | { |
| | 1 | 62 | | var startNode = pairs[0][0]; |
| | | 63 | | |
| | 6 | 64 | | foreach (var (start, startCount) in startToCountDictionary) |
| | 2 | 65 | | { |
| | 2 | 66 | | var endCount = endToCountDictionary.GetValueOrDefault(start); |
| | | 67 | | |
| | 2 | 68 | | if (startCount > endCount) |
| | 1 | 69 | | { |
| | 1 | 70 | | return start; |
| | | 71 | | } |
| | 1 | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | return startNode; |
| | 1 | 75 | | } |
| | | 76 | | |
| | | 77 | | private static void BuildEulerianPath(Dictionary<int, Stack<int>> startToEndsDictionary, int[][] pairs, int start, |
| | | 78 | | ref int pairsIndex) |
| | 13 | 79 | | { |
| | 13 | 80 | | if (!startToEndsDictionary.TryGetValue(start, out var ends)) |
| | 1 | 81 | | { |
| | 1 | 82 | | return; |
| | | 83 | | } |
| | | 84 | | |
| | 24 | 85 | | while (ends.Count > 0) |
| | 12 | 86 | | { |
| | 12 | 87 | | var end = ends.Pop(); |
| | | 88 | | |
| | 12 | 89 | | BuildEulerianPath(startToEndsDictionary, pairs, end, ref pairsIndex); |
| | | 90 | | |
| | 12 | 91 | | var pair = pairs[pairsIndex]; |
| | | 92 | | |
| | 12 | 93 | | pair[0] = start; |
| | 12 | 94 | | pair[1] = end; |
| | | 95 | | |
| | 12 | 96 | | pairsIndex--; |
| | 12 | 97 | | } |
| | 13 | 98 | | } |
| | | 99 | | } |