| | 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.PartitionArrayAccordingToGivenPivot; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class PartitionArrayAccordingToGivenPivotDynamicLists : IPartitionArrayAccordingToGivenPivot |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n) |
| | 19 | | /// Space complexity - O(n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="nums"></param> |
| | 22 | | /// <param name="pivot"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int[] PivotArray(int[] nums, int pivot) |
| 2 | 25 | | { |
| 2 | 26 | | var lessThanPivotList = new List<int>(); |
| 2 | 27 | | var equalToPivotList = new List<int>(); |
| 2 | 28 | | var greaterThanPivotList = new List<int>(); |
| | 29 | |
|
| 28 | 30 | | foreach (var num in nums) |
| 11 | 31 | | { |
| 11 | 32 | | if (num < pivot) |
| 4 | 33 | | { |
| 4 | 34 | | lessThanPivotList.Add(num); |
| 4 | 35 | | } |
| 7 | 36 | | else if (num > pivot) |
| 4 | 37 | | { |
| 4 | 38 | | greaterThanPivotList.Add(num); |
| 4 | 39 | | } |
| | 40 | | else |
| 3 | 41 | | { |
| 3 | 42 | | equalToPivotList.Add(num); |
| 3 | 43 | | } |
| 11 | 44 | | } |
| | 45 | |
|
| 2 | 46 | | var lessThanPivotListIndex = 0; |
| | 47 | |
|
| 6 | 48 | | while (lessThanPivotListIndex < lessThanPivotList.Count) |
| 4 | 49 | | { |
| 4 | 50 | | nums[lessThanPivotListIndex] = lessThanPivotList[lessThanPivotListIndex]; |
| | 51 | |
|
| 4 | 52 | | lessThanPivotListIndex++; |
| 4 | 53 | | } |
| | 54 | |
|
| 2 | 55 | | var equalToPivotListIndex = 0; |
| | 56 | |
|
| 5 | 57 | | while (equalToPivotListIndex < equalToPivotList.Count) |
| 3 | 58 | | { |
| 3 | 59 | | nums[equalToPivotListIndex + lessThanPivotListIndex] = equalToPivotList[equalToPivotListIndex]; |
| | 60 | |
|
| 3 | 61 | | equalToPivotListIndex++; |
| 3 | 62 | | } |
| | 63 | |
|
| 2 | 64 | | var greaterThanPivotListIndex = 0; |
| | 65 | |
|
| 6 | 66 | | while (greaterThanPivotListIndex < greaterThanPivotList.Count) |
| 4 | 67 | | { |
| 4 | 68 | | nums[greaterThanPivotListIndex + equalToPivotListIndex + lessThanPivotListIndex] = |
| 4 | 69 | | greaterThanPivotList[greaterThanPivotListIndex]; |
| | 70 | |
|
| 4 | 71 | | greaterThanPivotListIndex++; |
| 4 | 72 | | } |
| | 73 | |
|
| 2 | 74 | | return nums; |
| 2 | 75 | | } |
| | 76 | | } |