221 Maximum Square

这道题有两个思路, 一是沿用085的maximum rectangle的思路, 稍作改进即可, 代码如下, 这个方法运行192ms

class Solution:
    # @param {character[][]} matrix
    # @return {integer}
    def maximalSquare(self, matrix):
        ans = 0
        for i in range(0,len(matrix)):
            for j in range(0, len(matrix[0])):
                if i == 0:
                    matrix[i][j] = int(matrix[i][j])
                else:
                    if matrix[i][j] == "0":
                        matrix[i][j] = 0
                    else:
                        matrix[i][j] = matrix[i-1][j] + 1
        for m in matrix:
            ans = max(ans, self.largestRectangleArea(m))
        return ans

    def largestRectangleArea(self, height):
        ans = 0
        s = []
        for i in range(0, len(height)):
            left = i
            while (s != []) and (s[-1][0] > height[i]):
                left = s[-1][1]
                l = min(i - left, s[-1][0])
                ans = max(ans, l * l)
                s.pop()
            s.append([height[i], left])
        right = len(height)
        while s != []:
            left = s[-1][1]
            l = min(right - left, s[-1][0])
            ans = max(ans, l * l)
            s.pop()
        return ans

看到tag中有 dp, 试着用dp 做了一个 运行时间是200ms, 看出在复杂度都是O(n*n)的情况下 时间区别不大 代码如下

class Solution:
    # @param {character[][]} matrix
    # @return {integer}
    def maximalSquare(self, matrix):
        ans = 0
        m = len(matrix)
        if m == 0:
            return 0
        n = len(matrix[0])
        dp = [[0] * n for i in range(0,m)]
        for i in range(0,m):
            for j in range(0,n):
                dp[i][j] = int(matrix[i][j])
                if i and j and dp[i][j]:
                    dp[i][j] = 1 + min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1])
                    ans = max(ans, dp[i][j] * dp[i][j])
        return ans
时间: 2024-12-28 09:32:57

221 Maximum Square的相关文章

leetcode每日解题思路 221 Maximal Square

问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到, 这道题目的标签还是DP, 那么问题的关键就是要找到一个符合判断是否为正方形的递推式. 老套路, 先看基本型, 对于一个2*2的正方形,对于右下角的元素(1,1)而言, 他的上(0,1), 左(1,0), 左上(0,0)三个元素应该都是'1', 如此才能够组成一个合规的正方形: 那么如果是一个3*

Codeforces Round #599 (Div. 2) A. Maximum Square

Ujan decided to make a new wooden roof for the house. He has nn rectangular planks numbered from 11 to nn. The ii-th plank has size ai×1ai×1 (that is, the width is 11 and the height is aiai). Now, Ujan wants to make a square roof. He will first choos

221. Maximal Square

https://leetcode.com/problems/maximal-square/#/description Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0

[leedcode 221] Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. public class Solution { public int maximalSqu

221. Maximal Square java solutions

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. Credits:Special thanks to @Freezen for adding

Java for LeetCode 221 Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. 解题思路: dp问题,用一个dp[i][j]保存matrix[i][j]作为右下节点的时候

【LeetCode】221. Maximal Square

Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. Credits:Special thanks to @Fre

(medium)LeetCode 221.Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. Credits:Special thanks to @Freezen for adding

[LeetCode] 221. Maximal Square 最大正方形

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. Credits:Special thanks to @Freezen for addin