3195. Find the Minimum Area to Cover All Ones I LeetCode Solution

In this guide, you will get 3195. Find the Minimum Area to Cover All Ones I LeetCode Solution with the best time and space complexity. The solution to Find the Minimum Area to Cover All Ones I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find the Minimum Area to Cover All Ones I solution in C++
  4. Find the Minimum Area to Cover All Ones I solution in Java
  5. Find the Minimum Area to Cover All Ones I solution in Python
  6. Additional Resources
3195. Find the Minimum Area to Cover All Ones I LeetCode Solution image

Problem Statement of Find the Minimum Area to Cover All Ones I

You are given a 2D binary array grid. Find a rectangle with horizontal and vertical sides with the smallest area, such that all the 1’s in grid lie inside this rectangle.
Return the minimum possible area of the rectangle.

Example 1:

Input: grid = [[0,1,0],[1,0,1]]
Output: 6
Explanation:

The smallest rectangle has a height of 2 and a width of 3, so it has an area of 2 * 3 = 6.

Example 2:

Input: grid = [[1,0],[0,0]]
Output: 1
Explanation:

The smallest rectangle has both height and width 1, so its area is 1 * 1 = 1.

Constraints:

1 <= grid.length, grid[i].length <= 1000
grid[i][j] is either 0 or 1.
The input is generated such that there is at least one 1 in grid.

Complexity Analysis

  • Time Complexity: O(mn)
  • Space Complexity: O(1)

3195. Find the Minimum Area to Cover All Ones I LeetCode Solution in C++

class Solution {
 public:
  int minimumArea(vector<vector<int>>& grid) {
    int x1 = INT_MAX;
    int y1 = INT_MAX;
    int x2 = 0;
    int y2 = 0;

    for (int i = 0; i < grid.size(); ++i)
      for (int j = 0; j < grid[0].size(); ++j)
        if (grid[i][j] == 1) {
          x1 = min(x1, i);
          y1 = min(y1, j);
          x2 = max(x2, i);
          y2 = max(y2, j);
        }

    return (x2 - x1 + 1) * (y2 - y1 + 1);
  }
};
/* code provided by PROGIEZ */

3195. Find the Minimum Area to Cover All Ones I LeetCode Solution in Java

class Solution {
  public int minimumArea(int[][] grid) {
    int x1 = Integer.MAX_VALUE;
    int y1 = Integer.MAX_VALUE;
    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);
  }
}
// code provided by PROGIEZ

3195. Find the Minimum Area to Cover All Ones I LeetCode Solution in Python

class Solution:
  def minimumArea(self, grid: list[list[int]]) -> int:
    x1 = math.inf
    y1 = math.inf
    x2 = 0
    y2 = 0

    for i, row in enumerate(grid):
      for j, num in enumerate(row):
        if num == 1:
          x1 = min(x1, i)
          y1 = min(y1, j)
          x2 = max(x2, i)
          y2 = max(y2, j)

    return (x2 - x1 + 1) * (y2 - y1 + 1)
# code by PROGIEZ

Additional Resources

See also  1909. Remove One Element to Make the Array Strictly Increasing LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.