Untitled

 avatar
unknown
c_cpp
a year ago
729 B
4
Indexable
int getSmallestArea(vector<vector<int>> grid) {
    int m = grid.size();
    int n = grid[0].size();

    // Find the minimum and maximum row and column indices of 1s
    int minRow = m, maxRow = -1, minCol = n, maxCol = -1;

    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            if (grid[i][j] == 1) {
                minRow = min(minRow, i);
                maxRow = max(maxRow, i);
                minCol = min(minCol, j);
                maxCol = max(maxCol, j);
            }
        }
    }

    // Calculate the area of the smallest rectangle
    int height = maxRow - minRow + 1;
    int width = maxCol - minCol + 1;
    int area = height * width;

    return area;
}
Editor is loading...
Leave a Comment