3195. Find the Minimum Area to Cover All Ones I
UnknownView on LeetCode
Time: O(mn)
Space: O(1)
Advertisement
Approach
Find bounding box (min/max row and col) of all 1s; return area.
Key Techniques
Array
Array problems involve manipulating elements stored in a contiguous block of memory. Key techniques include two-pointer traversal, prefix sums, sliding windows, and in-place partitioning. In C#, arrays are zero-indexed and fixed in size — use List<T> when you need dynamic resizing.
Matrix
Matrix problems often involve BFS/DFS flood fill, dynamic programming on 2D grids, or spiral/diagonal traversal. For row × column DP, break it into 1D sub-problems column by column. Common pitfalls: boundary checks and modifying the input matrix in-place.
3195.cs
C#
// Approach: Find bounding box (min/max row and col) of all 1s; return area.
// Time: O(mn) Space: O(1)
public class Solution
{
public int MinimumArea(int[][] grid)
{
int x1 = int.MaxValue;
int y1 = int.MaxValue;
int x2 = 0;
int y2 = 0;
for (int i = 0; i < grid.Length; ++i)
{
for (int j = 0; j < grid[0].Length; ++j)
{
if (grid[i][j] == 1)
{
x1 = Math.Min(x1, i);
y1 = Math.Min(y1, j);
x2 = Math.Max(x2, i);
y2 = Math.Max(y2, j);
}
}
}
return (x2 - x1 + 1) * (y2 - y1 + 1);
}
}Advertisement
Was this solution helpful?