边工作边刷题:70天一遍leetcode: day 23-4

Max Rectangle

利用largest rectangle in histogram的方法,这题比较简单。
https://leetcode.com/discuss/97731/solution-largest-rectangle-histogram-solved-stack-simulation

class Solution:
    # @param {character[][]} matrix
    # @return {integer}
    def maximalRectangle(self, matrix):
        ret = 0
        if not matrix: return 0
        count = [0]* len(matrix[0]) + [-1]
        for row in matrix:
            for i in range(len(row)):
                count[i] += 1
                if row[i] == '0': count[i] = 0
            st = [(-1,-1)]
            for index, c in enumerate(count):
                while st[-1][1] > c:
                    ii, cc = st.pop()
                    ret = max(ret, cc * (index - st[-1][0] - 1))
                st.append((index, c))
        return ret
posted @ 2016-04-26 10:22  absolute100  阅读(75)  评论(0编辑  收藏  举报