Find the Minimum Area to Cover All Ones I

Medium
Watch on YouTube ↗

Solution

class Solution {
    public int minimumArea(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;

        int left = n-1, right = 0, top = m-1, bottom = 0;

        for(int i=0; i<m; i++) {
            for(int j=0; j<n; j++) {
                if(grid[i][j]==1) {
                    left = Math.min(left, j);
                    right = Math.max(right, j);
                    top = Math.min(top, i);
                    bottom = Math.max(bottom, i);
                }
            }
        }

        int area = (bottom-top+1) * (right-left+1);
        return area;
    }
}