LeetCode 85 Maximal Rectangle (C++, Python)

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.



Solution C++


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
    int maxHistoGram(vector<int>& heights) {
        heights.push_back(0);
        stack<int> increStack;
        int area = 0;
        for (int i = 0; i < heights.size(); ++i) {
            if (increStack.empty() || heights[i] > heights[increStack.top()]) {
                increStack.push(i);
            } else {
                int tmp = heights[increStack.top()];
                increStack.pop();
                area = max(area, tmp * (increStack.empty() ?i :i - increStack.top() - 1));
                --i;
            }
        }
        return area;
    }
public:
    int maximalRectangle(vector<vector<char> > &matrix) {
        int height = matrix.size();
        if (height == 0) return 0;
        int width = matrix[0].size();
        if (width == 0) return 0;
        vector<vector<int>> heightsHistoGrams(height, vector<int>(width, 0));
        int area = 0;
        for (int i = 0; i < height; ++i) {
            for (int j = 0; j < width; ++j) {
                if (matrix[i][j] == '0') continue;
                heightsHistoGrams[i][j] = 1;
                if (i > 0) heightsHistoGrams[i][j] += heightsHistoGrams[i - 1][j];
            }
        }
        for (int i = 0; i < height; ++i) {
            area = max(area, maxHistoGram(heightsHistoGrams[i]));
        }
        return area;
    }
};

Solution Python


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution:
    def maxHistoGram(self, heights):
        area = 0
        i = 0
        increStack = []
        while i < len(heights):
            if not increStack or heights[i] > heights[increStack[-1]]:
                increStack.append(i)
                i += 1
            else:
                tmp = heights[increStack.pop()]
                area = max(area, tmp * (i - increStack[-1] - 1 if (increStack) else i))
        return area
    # @param matrix, a list of lists of 1 length string
    # @return an integer
    def maximalRectangle(self, matrix):
        height = len(matrix)
        if not height: return 0
        width = len(matrix[0])
        if not width: return 0
        histoGramHeights = []
        for _ in range(height):
            histoGramHeights.append([0] * (width + 1))
        for i in range(height):
            for j in range(width):
                if matrix[i][j] == '0': continue
                histoGramHeights[i][j] = 1
                if i > 0: histoGramHeights[i][j] += histoGramHeights[i - 1][j]
        area = 0
        for i in range(height):
            area = max(area, self.maxHistoGram(histoGramHeights[i]))
        return area
        

Popular posts from this blog

How Django Works (4) URL Resolution

Python Class Method and Class Only Method

How Django works (3): Testing Server and Static Files Serving