< Summary

Information
Class: LeetCode.Algorithms.MaximalRectangle.MaximalRectangleStack
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaximalRectangle\MaximalRectangleStack.cs
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 59
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MaximalRectangle(...)100%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaximalRectangle\MaximalRectangleStack.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.MaximalRectangle;
 13
 14/// <inheritdoc />
 15public class MaximalRectangleStack : IMaximalRectangle
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n * m)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="matrix"></param>
 22    /// <returns></returns>
 23    public int MaximalRectangle(char[][] matrix)
 324    {
 325        var maxArea = 0;
 326        var height = new int[matrix[0].Length + 1];
 27
 2128        foreach (var item in matrix)
 629        {
 630            var stack = new Stack<int>();
 31
 6832            for (var col = 0; col <= matrix[0].Length; col++)
 2833            {
 2834                if (col < matrix[0].Length)
 2235                {
 2236                    if (item[col] == '1')
 1437                    {
 1438                        height[col] += 1;
 1439                    }
 40                    else
 841                    {
 842                        height[col] = 0;
 843                    }
 2244                }
 45
 4246                while (stack.Count > 0 && height[col] < height[stack.Peek()])
 1447                {
 1448                    var h = height[stack.Pop()];
 1449                    var w = stack.Count == 0 ? col : col - stack.Peek() - 1;
 1450                    maxArea = Math.Max(maxArea, h * w);
 1451                }
 52
 2853                stack.Push(col);
 2854            }
 655        }
 56
 357        return maxArea;
 358    }
 59}