| | 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.DesignFrontMiddleBackQueue; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class DesignFrontMiddleBackQueueList : IDesignFrontMiddleBackQueue |
| | 16 | | { |
| 1 | 17 | | private readonly List<int> _items = []; |
| | 18 | | private int _count; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Time complexity - O(n) |
| | 22 | | /// Space complexity - O(n) |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="value"></param> |
| | 25 | | public void PushFront(int value) |
| 1 | 26 | | { |
| 1 | 27 | | _items.Insert(0, value); |
| | 28 | |
|
| 1 | 29 | | _count++; |
| 1 | 30 | | } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Time complexity - O(n) |
| | 34 | | /// Space complexity - O(n) |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="value"></param> |
| | 37 | | public void PushMiddle(int value) |
| 2 | 38 | | { |
| 2 | 39 | | _items.Insert(_count / 2, value); |
| | 40 | |
|
| 2 | 41 | | _count++; |
| 2 | 42 | | } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Time complexity - O(1) |
| | 46 | | /// Space complexity - O(n) |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="value"></param> |
| | 49 | | public void PushBack(int value) |
| 1 | 50 | | { |
| 1 | 51 | | _items.Add(value); |
| | 52 | |
|
| 1 | 53 | | _count++; |
| 1 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Time complexity - O(n) |
| | 58 | | /// Space complexity - O(n) |
| | 59 | | /// </summary> |
| | 60 | | /// <returns></returns> |
| | 61 | | public int PopFront() |
| 2 | 62 | | { |
| 2 | 63 | | if (_count == 0) |
| 1 | 64 | | { |
| 1 | 65 | | return -1; |
| | 66 | | } |
| | 67 | |
|
| 1 | 68 | | var front = _items[0]; |
| | 69 | |
|
| 1 | 70 | | _items.RemoveAt(0); |
| | 71 | |
|
| 1 | 72 | | _count--; |
| | 73 | |
|
| 1 | 74 | | return front; |
| 2 | 75 | | } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Time complexity - O(n) |
| | 79 | | /// Space complexity - O(n) |
| | 80 | | /// </summary> |
| | 81 | | /// <returns></returns> |
| | 82 | | public int PopMiddle() |
| 2 | 83 | | { |
| 2 | 84 | | if (_count == 0) |
| 0 | 85 | | { |
| 0 | 86 | | return -1; |
| | 87 | | } |
| | 88 | |
|
| 2 | 89 | | var index = (_count - 1) / 2; |
| | 90 | |
|
| 2 | 91 | | var middle = _items[index]; |
| | 92 | |
|
| 2 | 93 | | _items.RemoveAt(index); |
| | 94 | |
|
| 2 | 95 | | _count--; |
| | 96 | |
|
| 2 | 97 | | return middle; |
| 2 | 98 | | } |
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// Time complexity - O(1) |
| | 102 | | /// Space complexity - O(n) |
| | 103 | | /// </summary> |
| | 104 | | /// <returns></returns> |
| | 105 | | public int PopBack() |
| 1 | 106 | | { |
| 1 | 107 | | if (_count == 0) |
| 0 | 108 | | { |
| 0 | 109 | | return -1; |
| | 110 | | } |
| | 111 | |
|
| 1 | 112 | | var index = _count - 1; |
| | 113 | |
|
| 1 | 114 | | var back = _items[index]; |
| | 115 | |
|
| 1 | 116 | | _items.RemoveAt(index); |
| | 117 | |
|
| 1 | 118 | | _count--; |
| | 119 | |
|
| 1 | 120 | | return back; |
| 1 | 121 | | } |
| | 122 | | } |