| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 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.RelativeSortArray; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class RelativeSortArrayTwoLoopsSorting : IRelativeSortArray |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(m * n + n log n) |
| | 19 | | /// Space complexity - O(log n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="arr1"></param> |
| | 22 | | /// <param name="arr2"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int[] RelativeSortArray(int[] arr1, int[] arr2) |
| 2 | 25 | | { |
| 2 | 26 | | var result = new List<int>(); |
| | 27 | |
|
| 26 | 28 | | foreach (var arr2Item in arr2) |
| 10 | 29 | | { |
| 200 | 30 | | for (var j = 0; j < arr1.Length; j++) |
| 90 | 31 | | { |
| 90 | 32 | | if (arr1[j] != arr2Item) |
| 77 | 33 | | { |
| 77 | 34 | | continue; |
| | 35 | | } |
| | 36 | |
|
| 13 | 37 | | result.Add(arr1[j]); |
| | 38 | |
|
| 13 | 39 | | arr1[j] = -1; |
| 13 | 40 | | } |
| 10 | 41 | | } |
| | 42 | |
|
| 2 | 43 | | Array.Sort(arr1); |
| | 44 | |
|
| 19 | 45 | | result.AddRange(arr1.Where(a => a >= 0)); |
| | 46 | |
|
| 2 | 47 | | return [.. result]; |
| 2 | 48 | | } |
| | 49 | | } |