| | 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 | |
|
| | 12 | | namespace LeetCode.Algorithms.FindTheMinimumAreaToCoverAllOnes2; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public 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) |
| 2 | 26 | | { |
| 2 | 27 | | var rowCount = grid.Length; |
| 2 | 28 | | var columnCount = grid[0].Length; |
| | 29 | |
|
| 2 | 30 | | var minimumSum = EmptyArea; |
| | 31 | |
|
| 2 | 32 | | minimumSum = Math.Min(minimumSum, MinimumTopBandThenBottomSplitByColumn(grid, rowCount, columnCount)); |
| 2 | 33 | | minimumSum = Math.Min(minimumSum, MinimumTopSplitByColumnThenBottomBand(grid, rowCount, columnCount)); |
| 2 | 34 | | minimumSum = Math.Min(minimumSum, MinimumThreeHorizontalBands(grid, rowCount, columnCount)); |
| 2 | 35 | | minimumSum = Math.Min(minimumSum, MinimumLeftBandThenRightSplitByRow(grid, rowCount, columnCount)); |
| 2 | 36 | | minimumSum = Math.Min(minimumSum, MinimumLeftSplitByRowThenRightBand(grid, rowCount, columnCount)); |
| 2 | 37 | | minimumSum = Math.Min(minimumSum, MinimumThreeVerticalBands(grid, rowCount, columnCount)); |
| | 38 | |
|
| 2 | 39 | | return minimumSum; |
| 2 | 40 | | } |
| | 41 | |
|
| | 42 | | private static int MinimumTopBandThenBottomSplitByColumn(int[][] grid, int rowCount, int columnCount) |
| 2 | 43 | | { |
| 2 | 44 | | var minimumSum = EmptyArea; |
| | 45 | |
|
| 8 | 46 | | for (var topEnd = 0; topEnd < rowCount - 1; topEnd++) |
| 2 | 47 | | { |
| 14 | 48 | | for (var splitColumn = 0; splitColumn < columnCount - 1; splitColumn++) |
| 5 | 49 | | { |
| 5 | 50 | | var currentSum = 0; |
| | 51 | |
|
| 5 | 52 | | currentSum += MinimumArea(grid, rowCount, columnCount, 0, topEnd, 0, columnCount - 1); |
| 5 | 53 | | currentSum += MinimumArea(grid, rowCount, columnCount, topEnd + 1, rowCount - 1, 0, splitColumn); |
| 5 | 54 | | currentSum += MinimumArea(grid, rowCount, columnCount, topEnd + 1, rowCount - 1, splitColumn + 1, |
| 5 | 55 | | columnCount - 1); |
| | 56 | |
|
| 5 | 57 | | if (currentSum < minimumSum) |
| 2 | 58 | | { |
| 2 | 59 | | minimumSum = currentSum; |
| 2 | 60 | | } |
| 5 | 61 | | } |
| 2 | 62 | | } |
| | 63 | |
|
| 2 | 64 | | return minimumSum; |
| 2 | 65 | | } |
| | 66 | |
|
| | 67 | | private static int MinimumTopSplitByColumnThenBottomBand(int[][] grid, int rowCount, int columnCount) |
| 2 | 68 | | { |
| 2 | 69 | | var minimumSum = EmptyArea; |
| | 70 | |
|
| 8 | 71 | | for (var topEnd = 0; topEnd < rowCount - 1; topEnd++) |
| 2 | 72 | | { |
| 14 | 73 | | for (var splitColumn = 0; splitColumn < columnCount - 1; splitColumn++) |
| 5 | 74 | | { |
| 5 | 75 | | var currentSum = 0; |
| | 76 | |
|
| 5 | 77 | | currentSum += MinimumArea(grid, rowCount, columnCount, 0, topEnd, 0, splitColumn); |
| 5 | 78 | | currentSum += MinimumArea(grid, rowCount, columnCount, 0, topEnd, splitColumn + 1, columnCount - 1); |
| 5 | 79 | | currentSum += MinimumArea(grid, rowCount, columnCount, topEnd + 1, rowCount - 1, 0, |
| 5 | 80 | | columnCount - 1); |
| | 81 | |
|
| 5 | 82 | | if (currentSum < minimumSum) |
| 2 | 83 | | { |
| 2 | 84 | | minimumSum = currentSum; |
| 2 | 85 | | } |
| 5 | 86 | | } |
| 2 | 87 | | } |
| | 88 | |
|
| 2 | 89 | | return minimumSum; |
| 2 | 90 | | } |
| | 91 | |
|
| | 92 | | private static int MinimumThreeHorizontalBands(int[][] grid, int rowCount, int columnCount) |
| 2 | 93 | | { |
| 2 | 94 | | var minimumSum = EmptyArea; |
| | 95 | |
|
| 4 | 96 | | for (var topEnd = 0; topEnd < rowCount - 2; topEnd++) |
| 0 | 97 | | { |
| 0 | 98 | | for (var midEnd = topEnd + 1; midEnd < rowCount - 1; midEnd++) |
| 0 | 99 | | { |
| 0 | 100 | | var currentSum = 0; |
| | 101 | |
|
| 0 | 102 | | currentSum += MinimumArea(grid, rowCount, columnCount, 0, topEnd, 0, columnCount - 1); |
| 0 | 103 | | currentSum += MinimumArea(grid, rowCount, columnCount, topEnd + 1, midEnd, 0, columnCount - 1); |
| 0 | 104 | | currentSum += MinimumArea(grid, rowCount, columnCount, midEnd + 1, rowCount - 1, 0, columnCount - 1); |
| | 105 | |
|
| 0 | 106 | | if (currentSum < minimumSum) |
| 0 | 107 | | { |
| 0 | 108 | | minimumSum = currentSum; |
| 0 | 109 | | } |
| 0 | 110 | | } |
| 0 | 111 | | } |
| | 112 | |
|
| 2 | 113 | | return minimumSum; |
| 2 | 114 | | } |
| | 115 | |
|
| | 116 | | private static int MinimumLeftBandThenRightSplitByRow(int[][] grid, int rowCount, int columnCount) |
| 2 | 117 | | { |
| 2 | 118 | | var minimumSum = EmptyArea; |
| | 119 | |
|
| 14 | 120 | | for (var leftEnd = 0; leftEnd < columnCount - 1; leftEnd++) |
| 5 | 121 | | { |
| 20 | 122 | | for (var splitRow = 0; splitRow < rowCount - 1; splitRow++) |
| 5 | 123 | | { |
| 5 | 124 | | var currentSum = 0; |
| | 125 | |
|
| 5 | 126 | | currentSum += MinimumArea(grid, rowCount, columnCount, 0, rowCount - 1, 0, leftEnd); |
| 5 | 127 | | currentSum += MinimumArea(grid, rowCount, columnCount, 0, splitRow, leftEnd + 1, columnCount - 1); |
| 5 | 128 | | currentSum += MinimumArea(grid, rowCount, columnCount, splitRow + 1, rowCount - 1, leftEnd + 1, |
| 5 | 129 | | columnCount - 1); |
| | 130 | |
|
| 5 | 131 | | if (currentSum < minimumSum) |
| 2 | 132 | | { |
| 2 | 133 | | minimumSum = currentSum; |
| 2 | 134 | | } |
| 5 | 135 | | } |
| 5 | 136 | | } |
| | 137 | |
|
| 2 | 138 | | return minimumSum; |
| 2 | 139 | | } |
| | 140 | |
|
| | 141 | | private static int MinimumLeftSplitByRowThenRightBand(int[][] grid, int rowCount, int columnCount) |
| 2 | 142 | | { |
| 2 | 143 | | var minimumSum = EmptyArea; |
| | 144 | |
|
| 14 | 145 | | for (var leftEnd = 0; leftEnd < columnCount - 1; leftEnd++) |
| 5 | 146 | | { |
| 20 | 147 | | for (var splitRow = 0; splitRow < rowCount - 1; splitRow++) |
| 5 | 148 | | { |
| 5 | 149 | | var currentSum = 0; |
| | 150 | |
|
| 5 | 151 | | currentSum += MinimumArea(grid, rowCount, columnCount, 0, splitRow, 0, leftEnd); |
| 5 | 152 | | currentSum += MinimumArea(grid, rowCount, columnCount, splitRow + 1, rowCount - 1, 0, leftEnd); |
| 5 | 153 | | currentSum += MinimumArea(grid, rowCount, columnCount, 0, rowCount - 1, leftEnd + 1, |
| 5 | 154 | | columnCount - 1); |
| | 155 | |
|
| 5 | 156 | | if (currentSum < minimumSum) |
| 4 | 157 | | { |
| 4 | 158 | | minimumSum = currentSum; |
| 4 | 159 | | } |
| 5 | 160 | | } |
| 5 | 161 | | } |
| | 162 | |
|
| 2 | 163 | | return minimumSum; |
| 2 | 164 | | } |
| | 165 | |
|
| | 166 | | private static int MinimumThreeVerticalBands(int[][] grid, int rowCount, int columnCount) |
| 2 | 167 | | { |
| 2 | 168 | | var minimumSum = EmptyArea; |
| | 169 | |
|
| 10 | 170 | | for (var leftEnd = 0; leftEnd < columnCount - 2; leftEnd++) |
| 3 | 171 | | { |
| 14 | 172 | | for (var midEnd = leftEnd + 1; midEnd < columnCount - 1; midEnd++) |
| 4 | 173 | | { |
| 4 | 174 | | var currentSum = 0; |
| | 175 | |
|
| 4 | 176 | | currentSum += MinimumArea(grid, rowCount, columnCount, 0, rowCount - 1, 0, leftEnd); |
| 4 | 177 | | currentSum += MinimumArea(grid, rowCount, columnCount, 0, rowCount - 1, leftEnd + 1, midEnd); |
| 4 | 178 | | currentSum += MinimumArea(grid, rowCount, columnCount, 0, rowCount - 1, midEnd + 1, columnCount - 1); |
| | 179 | |
|
| 4 | 180 | | if (currentSum < minimumSum) |
| 2 | 181 | | { |
| 2 | 182 | | minimumSum = currentSum; |
| 2 | 183 | | } |
| 4 | 184 | | } |
| 3 | 185 | | } |
| | 186 | |
|
| 2 | 187 | | return minimumSum; |
| 2 | 188 | | } |
| | 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) |
| 72 | 198 | | { |
| 72 | 199 | | var minRow = rowCount; |
| 72 | 200 | | var maxRow = 0; |
| 72 | 201 | | var minColumn = columnCount; |
| 72 | 202 | | var maxColumn = 0; |
| | 203 | |
|
| 332 | 204 | | for (var row = rowStart; row <= rowEnd; row++) |
| 94 | 205 | | { |
| 536 | 206 | | for (var column = columnStart; column <= columnEnd; column++) |
| 174 | 207 | | { |
| 174 | 208 | | if (grid[row][column] == 0) |
| 69 | 209 | | { |
| 69 | 210 | | continue; |
| | 211 | | } |
| | 212 | |
|
| 105 | 213 | | if (row < minRow) |
| 68 | 214 | | { |
| 68 | 215 | | minRow = row; |
| 68 | 216 | | } |
| | 217 | |
|
| 105 | 218 | | if (row > maxRow) |
| 41 | 219 | | { |
| 41 | 220 | | maxRow = row; |
| 41 | 221 | | } |
| | 222 | |
|
| 105 | 223 | | if (column < minColumn) |
| 71 | 224 | | { |
| 71 | 225 | | minColumn = column; |
| 71 | 226 | | } |
| | 227 | |
|
| 105 | 228 | | if (column > maxColumn) |
| 65 | 229 | | { |
| 65 | 230 | | maxColumn = column; |
| 65 | 231 | | } |
| 105 | 232 | | } |
| 94 | 233 | | } |
| | 234 | |
|
| 72 | 235 | | if (maxRow < minRow) |
| 4 | 236 | | { |
| 4 | 237 | | return EmptyArea; |
| | 238 | | } |
| | 239 | |
|
| 68 | 240 | | return (maxRow - minRow + 1) * (maxColumn - minColumn + 1); |
| 72 | 241 | | } |
| | 242 | | } |