| | | 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.ImplementQueueUsingStacks; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class ImplementQueueUsingStacksTwoStacks : IImplementQueueUsingStacks |
| | | 16 | | { |
| | 9 | 17 | | private readonly Stack<int> _stack = 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) |
| | 14 | 25 | | { |
| | 14 | 26 | | var tempStack = new Stack<int>(); |
| | | 27 | | |
| | 26 | 28 | | while (_stack.Count > 0) |
| | 12 | 29 | | { |
| | 12 | 30 | | tempStack.Push(_stack.Pop()); |
| | 12 | 31 | | } |
| | | 32 | | |
| | 14 | 33 | | _stack.Push(x); |
| | | 34 | | |
| | 26 | 35 | | while (tempStack.Count > 0) |
| | 12 | 36 | | { |
| | 12 | 37 | | _stack.Push(tempStack.Pop()); |
| | 12 | 38 | | } |
| | 14 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Time complexity - O(1) |
| | | 43 | | /// Space complexity - O(1) |
| | | 44 | | /// </summary> |
| | | 45 | | /// <returns></returns> |
| | | 46 | | public int Pop() |
| | 8 | 47 | | { |
| | 8 | 48 | | return _stack.Pop(); |
| | 7 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Time complexity - O(1) |
| | | 53 | | /// Space complexity - O(1) |
| | | 54 | | /// </summary> |
| | | 55 | | /// <returns></returns> |
| | | 56 | | public int Peek() |
| | 3 | 57 | | { |
| | 3 | 58 | | return _stack.Peek(); |
| | 2 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Time complexity - O(1) |
| | | 63 | | /// Space complexity - O(1) |
| | | 64 | | /// </summary> |
| | | 65 | | /// <returns></returns> |
| | | 66 | | public bool Empty() |
| | 7 | 67 | | { |
| | 7 | 68 | | return _stack.Count == 0; |
| | 7 | 69 | | } |
| | | 70 | | } |