2025-08-22 Daily Challenge
Today I have done leetcode's August LeetCoding Challenge with cpp
.
August LeetCoding Challenge 22
Description
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
.
Solution
class Solution {
public:
int minimumArea(vector<vector<int>>& grid) {
int minX = INT_MAX;
int minY = INT_MAX;
int maxX = INT_MIN;
int maxY = INT_MIN;
int x = grid.size();
int y = grid.front().size();
for(int i = 0; i < x; ++i) {
for(int j = 0; j < y; ++j) {
if(!grid[i][j]) continue;
minX = min(i, minX);
minY = min(j, minY);
maxX = max(i, maxX);
maxY = max(j, maxY);
}
}
return (maxX - minX + 1) * (maxY - minY + 1);
}
};
// Accepted
// 712/712 cases passed (255 ms)
// Your runtime beats 66.16 % of cpp submissions
// Your memory usage beats 97.58 % of cpp submissions (133.3 MB)