< Summary

Information
Class: LeetCode.Algorithms.FindTheMinimumAreaToCoverAllOnes2.FindTheMinimumAreaToCoverAllOnes2BruteForce
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheMinimumAreaToCoverAllOnes2\FindTheMinimumAreaToCoverAllOnes2BruteForce.cs
Line coverage
91%
Covered lines: 146
Uncovered lines: 13
Coverable lines: 159
Total lines: 242
Line coverage: 91.8%
Branch coverage
90%
Covered branches: 47
Total branches: 52
Branch coverage: 90.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheMinimumAreaToCoverAllOnes2\FindTheMinimumAreaToCoverAllOnes2BruteForce.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.FindTheMinimumAreaToCoverAllOnes2;
 13
 14/// <inheritdoc />
 15public class FindTheMinimumAreaToCoverAllOnes2BruteForce : IFindTheMinimumAreaToCoverAllOnes2
 16{
 17    private const int EmptyArea = int.MaxValue / 3;
 18
 19    /// <summary>
 20    ///     Time complexity - O(m^2 * n^2)
 21    ///     Space complexity - O(1)
 22    /// </summary>
 23    /// <param name="grid"></param>
 24    /// <returns></returns>
 25    public int MinimumSum(int[][] grid)
 226    {
 227        var rowCount = grid.Length;
 228        var columnCount = grid[0].Length;
 29
 230        var minimumSum = EmptyArea;
 31
 232        minimumSum = Math.Min(minimumSum, MinimumTopBandThenBottomSplitByColumn(grid, rowCount, columnCount));
 233        minimumSum = Math.Min(minimumSum, MinimumTopSplitByColumnThenBottomBand(grid, rowCount, columnCount));
 234        minimumSum = Math.Min(minimumSum, MinimumThreeHorizontalBands(grid, rowCount, columnCount));
 235        minimumSum = Math.Min(minimumSum, MinimumLeftBandThenRightSplitByRow(grid, rowCount, columnCount));
 236        minimumSum = Math.Min(minimumSum, MinimumLeftSplitByRowThenRightBand(grid, rowCount, columnCount));
 237        minimumSum = Math.Min(minimumSum, MinimumThreeVerticalBands(grid, rowCount, columnCount));
 38
 239        return minimumSum;
 240    }
 41
 42    private static int MinimumTopBandThenBottomSplitByColumn(int[][] grid, int rowCount, int columnCount)
 243    {
 244        var minimumSum = EmptyArea;
 45
 846        for (var topEnd = 0; topEnd < rowCount - 1; topEnd++)
 247        {
 1448            for (var splitColumn = 0; splitColumn < columnCount - 1; splitColumn++)
 549            {
 550                var currentSum = 0;
 51
 552                currentSum += MinimumArea(grid, rowCount, columnCount, 0, topEnd, 0, columnCount - 1);
 553                currentSum += MinimumArea(grid, rowCount, columnCount, topEnd + 1, rowCount - 1, 0, splitColumn);
 554                currentSum += MinimumArea(grid, rowCount, columnCount, topEnd + 1, rowCount - 1, splitColumn + 1,
 555                    columnCount - 1);
 56
 557                if (currentSum < minimumSum)
 258                {
 259                    minimumSum = currentSum;
 260                }
 561            }
 262        }
 63
 264        return minimumSum;
 265    }
 66
 67    private static int MinimumTopSplitByColumnThenBottomBand(int[][] grid, int rowCount, int columnCount)
 268    {
 269        var minimumSum = EmptyArea;
 70
 871        for (var topEnd = 0; topEnd < rowCount - 1; topEnd++)
 272        {
 1473            for (var splitColumn = 0; splitColumn < columnCount - 1; splitColumn++)
 574            {
 575                var currentSum = 0;
 76
 577                currentSum += MinimumArea(grid, rowCount, columnCount, 0, topEnd, 0, splitColumn);
 578                currentSum += MinimumArea(grid, rowCount, columnCount, 0, topEnd, splitColumn + 1, columnCount - 1);
 579                currentSum += MinimumArea(grid, rowCount, columnCount, topEnd + 1, rowCount - 1, 0,
 580                    columnCount - 1);
 81
 582                if (currentSum < minimumSum)
 283                {
 284                    minimumSum = currentSum;
 285                }
 586            }
 287        }
 88
 289        return minimumSum;
 290    }
 91
 92    private static int MinimumThreeHorizontalBands(int[][] grid, int rowCount, int columnCount)
 293    {
 294        var minimumSum = EmptyArea;
 95
 496        for (var topEnd = 0; topEnd < rowCount - 2; topEnd++)
 097        {
 098            for (var midEnd = topEnd + 1; midEnd < rowCount - 1; midEnd++)
 099            {
 0100                var currentSum = 0;
 101
 0102                currentSum += MinimumArea(grid, rowCount, columnCount, 0, topEnd, 0, columnCount - 1);
 0103                currentSum += MinimumArea(grid, rowCount, columnCount, topEnd + 1, midEnd, 0, columnCount - 1);
 0104                currentSum += MinimumArea(grid, rowCount, columnCount, midEnd + 1, rowCount - 1, 0, columnCount - 1);
 105
 0106                if (currentSum < minimumSum)
 0107                {
 0108                    minimumSum = currentSum;
 0109                }
 0110            }
 0111        }
 112
 2113        return minimumSum;
 2114    }
 115
 116    private static int MinimumLeftBandThenRightSplitByRow(int[][] grid, int rowCount, int columnCount)
 2117    {
 2118        var minimumSum = EmptyArea;
 119
 14120        for (var leftEnd = 0; leftEnd < columnCount - 1; leftEnd++)
 5121        {
 20122            for (var splitRow = 0; splitRow < rowCount - 1; splitRow++)
 5123            {
 5124                var currentSum = 0;
 125
 5126                currentSum += MinimumArea(grid, rowCount, columnCount, 0, rowCount - 1, 0, leftEnd);
 5127                currentSum += MinimumArea(grid, rowCount, columnCount, 0, splitRow, leftEnd + 1, columnCount - 1);
 5128                currentSum += MinimumArea(grid, rowCount, columnCount, splitRow + 1, rowCount - 1, leftEnd + 1,
 5129                    columnCount - 1);
 130
 5131                if (currentSum < minimumSum)
 2132                {
 2133                    minimumSum = currentSum;
 2134                }
 5135            }
 5136        }
 137
 2138        return minimumSum;
 2139    }
 140
 141    private static int MinimumLeftSplitByRowThenRightBand(int[][] grid, int rowCount, int columnCount)
 2142    {
 2143        var minimumSum = EmptyArea;
 144
 14145        for (var leftEnd = 0; leftEnd < columnCount - 1; leftEnd++)
 5146        {
 20147            for (var splitRow = 0; splitRow < rowCount - 1; splitRow++)
 5148            {
 5149                var currentSum = 0;
 150
 5151                currentSum += MinimumArea(grid, rowCount, columnCount, 0, splitRow, 0, leftEnd);
 5152                currentSum += MinimumArea(grid, rowCount, columnCount, splitRow + 1, rowCount - 1, 0, leftEnd);
 5153                currentSum += MinimumArea(grid, rowCount, columnCount, 0, rowCount - 1, leftEnd + 1,
 5154                    columnCount - 1);
 155
 5156                if (currentSum < minimumSum)
 4157                {
 4158                    minimumSum = currentSum;
 4159                }
 5160            }
 5161        }
 162
 2163        return minimumSum;
 2164    }
 165
 166    private static int MinimumThreeVerticalBands(int[][] grid, int rowCount, int columnCount)
 2167    {
 2168        var minimumSum = EmptyArea;
 169
 10170        for (var leftEnd = 0; leftEnd < columnCount - 2; leftEnd++)
 3171        {
 14172            for (var midEnd = leftEnd + 1; midEnd < columnCount - 1; midEnd++)
 4173            {
 4174                var currentSum = 0;
 175
 4176                currentSum += MinimumArea(grid, rowCount, columnCount, 0, rowCount - 1, 0, leftEnd);
 4177                currentSum += MinimumArea(grid, rowCount, columnCount, 0, rowCount - 1, leftEnd + 1, midEnd);
 4178                currentSum += MinimumArea(grid, rowCount, columnCount, 0, rowCount - 1, midEnd + 1, columnCount - 1);
 179
 4180                if (currentSum < minimumSum)
 2181                {
 2182                    minimumSum = currentSum;
 2183                }
 4184            }
 3185        }
 186
 2187        return minimumSum;
 2188    }
 189
 190    private static int MinimumArea(
 191        int[][] grid,
 192        int rowCount,
 193        int columnCount,
 194        int rowStart,
 195        int rowEnd,
 196        int columnStart,
 197        int columnEnd)
 72198    {
 72199        var minRow = rowCount;
 72200        var maxRow = 0;
 72201        var minColumn = columnCount;
 72202        var maxColumn = 0;
 203
 332204        for (var row = rowStart; row <= rowEnd; row++)
 94205        {
 536206            for (var column = columnStart; column <= columnEnd; column++)
 174207            {
 174208                if (grid[row][column] == 0)
 69209                {
 69210                    continue;
 211                }
 212
 105213                if (row < minRow)
 68214                {
 68215                    minRow = row;
 68216                }
 217
 105218                if (row > maxRow)
 41219                {
 41220                    maxRow = row;
 41221                }
 222
 105223                if (column < minColumn)
 71224                {
 71225                    minColumn = column;
 71226                }
 227
 105228                if (column > maxColumn)
 65229                {
 65230                    maxColumn = column;
 65231                }
 105232            }
 94233        }
 234
 72235        if (maxRow < minRow)
 4236        {
 4237            return EmptyArea;
 238        }
 239
 68240        return (maxRow - minRow + 1) * (maxColumn - minColumn + 1);
 72241    }
 242}