| | 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.ImplementStackUsingQueues; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class ImplementStackUsingQueuesOneQueue : IImplementStackUsingQueues |
| | 16 | | { |
| 7 | 17 | | private readonly Queue<int> _queue = new(); |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(n) |
| | 21 | | /// Space complexity - O(n) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="x"></param> |
| | 24 | | public void Push(int x) |
| 15 | 25 | | { |
| 15 | 26 | | var size = _queue.Count; |
| | 27 | |
|
| 15 | 28 | | _queue.Enqueue(x); |
| | 29 | |
|
| 60 | 30 | | for (var i = 0; i < size; i++) |
| 15 | 31 | | { |
| 15 | 32 | | _queue.Enqueue(_queue.Dequeue()); |
| 15 | 33 | | } |
| 15 | 34 | | } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Time complexity - O(1) |
| | 38 | | /// Space complexity - O(n) |
| | 39 | | /// </summary> |
| | 40 | | /// <returns></returns> |
| | 41 | | public int Pop() |
| 9 | 42 | | { |
| 9 | 43 | | return _queue.Dequeue(); |
| 9 | 44 | | } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Time complexity - O(1) |
| | 48 | | /// Space complexity - O(n) |
| | 49 | | /// </summary> |
| | 50 | | /// <returns></returns> |
| | 51 | | public int Top() |
| 5 | 52 | | { |
| 5 | 53 | | return _queue.Peek(); |
| 5 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Time complexity - O(1) |
| | 58 | | /// Space complexity - O(n) |
| | 59 | | /// </summary> |
| | 60 | | /// <returns></returns> |
| | 61 | | public bool Empty() |
| 4 | 62 | | { |
| 4 | 63 | | return _queue.Count == 0; |
| 4 | 64 | | } |
| | 65 | | } |