An easy brute force way to solve the problem


  • 0
    W

    As the black area is connected, just find the most left,right,top and bottom point which is '1'.

    //O(M*N),O(1)
    class Solution {
    public:
        int minArea(vector<vector<char>>& image, int x, int y) {
            if(!image.size() || !image[0].size()) return 0;
            int row = image.size(), col = image[0].size();
            int minc = col, maxc = 0, minr = row, maxr = 0;
            for(int i=0;i<row;i++){
                for(int j=0;j<col;j++){
                    if(image[i][j] == '1'){
                        minc = min(minc,j);
                        minr = min(minr,i);
                        maxc = max(maxc,j);
                        maxr = max(maxr,i);
                    }
                }
            }
            
            return (maxc - minc + 1) * (maxr - minr + 1);
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.