< Summary

Information
Class: LeetCode.Algorithms.MaximumWidthRamp.MaximumWidthRampStack
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaximumWidthRamp\MaximumWidthRampStack.cs
Line coverage
92%
Covered lines: 23
Uncovered lines: 2
Coverable lines: 25
Total lines: 57
Line coverage: 92%
Branch coverage
87%
Covered branches: 14
Total branches: 16
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MaxWidthRamp(...)87.5%161692%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaximumWidthRamp\MaximumWidthRampStack.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.MaximumWidthRamp;
 13
 14/// <inheritdoc />
 15public class MaximumWidthRampStack : IMaximumWidthRamp
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <returns></returns>
 23    public int MaxWidthRamp(int[] nums)
 324    {
 325        var stack = new Stack<int>();
 26
 9827        for (var i = 0; i < nums.Length; i++)
 4628        {
 4629            if (stack.Count == 0 || nums[i] < nums[stack.Peek()])
 1630            {
 1631                stack.Push(i);
 1632            }
 4633        }
 34
 335        var ramp = 0;
 36
 3637        for (var i = nums.Length - 1; i >= 0; i--)
 1838        {
 1839            if (i <= ramp)
 340            {
 341                break;
 42            }
 43
 2344            while (stack.Count > 0 && nums[i] >= nums[stack.Peek()])
 845            {
 846                ramp = Math.Max(ramp, i - stack.Pop());
 847            }
 48
 1549            if (stack.Count == 0)
 050            {
 051                break;
 52            }
 1553        }
 54
 355        return ramp;
 356    }
 57}

Methods/Properties

MaxWidthRamp(System.Int32[])