| | 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.FindTheMinimumAreaToCoverAllOnes1; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class FindTheMinimumAreaToCoverAllOnes1OnePass : IFindTheMinimumAreaToCoverAllOnes1 |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(m * n) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="grid"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int MinimumArea(int[][] grid) |
| 5 | 24 | | { |
| 5 | 25 | | var m = grid.Length; |
| 5 | 26 | | var n = grid[0].Length; |
| | 27 | |
|
| 5 | 28 | | var top = m; |
| 5 | 29 | | var bottom = -1; |
| 5 | 30 | | var left = n; |
| 5 | 31 | | var right = -1; |
| | 32 | |
|
| 44 | 33 | | for (var i = 0; i < m; i++) |
| 17 | 34 | | { |
| 168 | 35 | | for (var j = 0; j < n; j++) |
| 67 | 36 | | { |
| 67 | 37 | | if (grid[i][j] == 0) |
| 56 | 38 | | { |
| 56 | 39 | | continue; |
| | 40 | | } |
| | 41 | |
|
| 11 | 42 | | if (i < top) |
| 5 | 43 | | { |
| 5 | 44 | | top = i; |
| 5 | 45 | | } |
| | 46 | |
|
| 11 | 47 | | if (i > bottom) |
| 9 | 48 | | { |
| 9 | 49 | | bottom = i; |
| 9 | 50 | | } |
| | 51 | |
|
| 11 | 52 | | if (j < left) |
| 8 | 53 | | { |
| 8 | 54 | | left = j; |
| 8 | 55 | | } |
| | 56 | |
|
| 11 | 57 | | if (j > right) |
| 7 | 58 | | { |
| 7 | 59 | | right = j; |
| 7 | 60 | | } |
| 11 | 61 | | } |
| 17 | 62 | | } |
| | 63 | |
|
| 5 | 64 | | if (bottom < 0) |
| 0 | 65 | | { |
| 0 | 66 | | return 0; |
| | 67 | | } |
| | 68 | |
|
| 5 | 69 | | return (right - left + 1) * (bottom - top + 1); |
| 5 | 70 | | } |
| | 71 | | } |