leetcode-85-最大矩形

            题目描述:

            方法一:动态规划+使用柱状图的优化暴力方法 O(N*2M) O(NM) N为行数

            class Solution:
                def maximalRectangle(self, matrix: List[List[str]]) -> int:
                    maxarea = 0
            
                    dp = [[0] * len(matrix[0]) for _ in range(len(matrix))]
                    for i in range(len(matrix)):
                        for j in range(len(matrix[0])):
                            if matrix[i][j] == 0: continue
            
                            # compute the maximum width and update dp with it
                            width = dp[i][j] = dp[i][j-1] + 1 if j else 1
            
                            # compute the maximum area rectangle with a lower right corner at [i, j]
                            for k in range(i, -1, -1):
                                width = min(width, dp[k][j])
                                maxarea = max(maxarea, width * (i-k+1))
                    return maxarea

            方法二:栈 参考84题 O(NM) O(M)

            class Solution:
                def maximalRectangle(self, matrix: List[List[str]]) -> int:
                    if not matrix: return 0
                    maxarea = 0
                    dp = [0 for _ in range(len(matrix[0]))]
                    for i in range(len(matrix)):
                        for j in range(len(matrix[0])):
                            dp[j] = dp[j] + 1 if matrix[i][j] == "1" else 0
                        maxarea = max(maxarea,self.largestRectangleArea(dp))
                    return maxarea
            
                def largestRectangleArea(self, heights: List[int]) -> int:
                    stack = [0]
                    heights = [0] + heights + [0]
                    res = 0
                    for i in range(len(heights)):
                        while heights[stack[-1]] > heights[i]:
                            tmp = stack.pop()
                            res = max(res, (i - stack[-1] - 1) * heights[tmp])
                        stack.append(i)
                    return res

            方法三:动态规划  O(NM)

            class Solution:
                def maximalRectangle(self, matrix: List[List[str]]) -> int:
                    if not matrix or not matrix[0]: return 0
                    row = len(matrix)
                    col = len(matrix[0])
                    left_j = [-1] * col
                    right_j = [col] * col
                    height_j = [0] * col
                    res = 0
                    for i in range(row):
                        cur_left = -1
                        cur_right = col
            
                        for j in range(col):
                            if matrix[i][j] == "1":
                                height_j[j] += 1
                            else:
                                height_j[j] = 0
            
                        for j in range(col):
                            if matrix[i][j] == "1":
                                left_j[j] = max(left_j[j], cur_left)
                            else:
                                left_j[j] = -1
                                cur_left = j
            
                        for j in range(col - 1, -1, -1):
                            if matrix[i][j] == "1":
                                right_j[j] = min(right_j[j], cur_right)
                            else:
                                right_j[j] = col
                                cur_right = j
                        for j in range(col):
                            res = max(res, (right_j[j] - left_j[j] - 1) * height_j[j])
                    return res
            相关文章
            相关标签/搜索
            王中王鉄算盘开奖结果小说 古丈县| 洪雅县| 顺义区| 宜兰市| 新密市| 荆门市| 廉江市| 夏津县| 潍坊市| 庄河市| 江孜县| 安陆市| 阳新县| 长治市| 民乐县| 上思县| 谢通门县| 建瓯市| 盐山县| 兰溪市| 石门县| 丰都县| 绵竹市| 名山县| 郎溪县| 鸡东县| 玛曲县| 南丹县| 富阳市| 清涧县| 汉源县| 博客| 师宗县| 汤原县| 泰安市| 涪陵区| 澄迈县| http://fa.hz0j1r7vo.fun http://fa.hz0j1r1vo.fun http://fa.hz0j0r9vo.fun http://fa.hz0j1r2vo.fun http://fa.hz0j2r9vo.fun