< Summary

Information
Class: LeetCode.Algorithms.ImplementQueueUsingStacks.ImplementQueueUsingStacksAmortized
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ImplementQueueUsingStacks\ImplementQueueUsingStacksAmortized.cs
Line coverage
92%
Covered lines: 26
Uncovered lines: 2
Coverable lines: 28
Total lines: 79
Line coverage: 92.8%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
Push(...)100%11100%
Pop()100%44100%
Peek()75%4480%
Empty()100%22100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ImplementQueueUsingStacks\ImplementQueueUsingStacksAmortized.cs

#LineLine coverage
 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
 12namespace LeetCode.Algorithms.ImplementQueueUsingStacks;
 13
 14/// <inheritdoc />
 15public class ImplementQueueUsingStacksAmortized : IImplementQueueUsingStacks
 16{
 917    private readonly Stack<int> _dequeueStack = new();
 918    private readonly Stack<int> _enqueueStack = new();
 19
 20    /// <summary>
 21    ///     Time complexity - O(1)
 22    ///     Space complexity - O(n)
 23    /// </summary>
 24    /// <param name="x"></param>
 25    public void Push(int x)
 1426    {
 1427        _enqueueStack.Push(x);
 1428    }
 29
 30    /// <summary>
 31    ///     Time complexity - Amortized O(1), worst case - O(n)
 32    ///     Space complexity - O(1)
 33    /// </summary>
 34    /// <returns></returns>
 35    public int Pop()
 836    {
 837        if (_dequeueStack.Count != 0)
 438        {
 439            return _dequeueStack.Pop();
 40        }
 41
 1142        while (_enqueueStack.Count > 0)
 743        {
 744            _dequeueStack.Push(_enqueueStack.Pop());
 745        }
 46
 447        return _dequeueStack.Pop();
 748    }
 49
 50    /// <summary>
 51    ///     Time complexity - Amortized O(1), worst case - O(n)
 52    ///     Space complexity - O(1)
 53    /// </summary>
 54    /// <returns></returns>
 55    public int Peek()
 356    {
 357        if (_dequeueStack.Count != 0)
 058        {
 059            return _dequeueStack.Peek();
 60        }
 61
 962        while (_enqueueStack.Count > 0)
 663        {
 664            _dequeueStack.Push(_enqueueStack.Pop());
 665        }
 66
 367        return _dequeueStack.Peek();
 268    }
 69
 70    /// <summary>
 71    ///     Time complexity - O(1)
 72    ///     Space complexity - O(1)
 73    /// </summary>
 74    /// <returns></returns>
 75    public bool Empty()
 776    {
 777        return _enqueueStack.Count == 0 && _dequeueStack.Count == 0;
 778    }
 79}