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 1 0

Return 4.

Sol 1:

Brute Force.

class Solution(object):
    def maximalSquare(self, matrix):
        """
        :type matrix: List[List[str]]
        :rtype: int
        """
        # brute force

        if len(matrix) <= 0 :
            return 0

        rows = len(matrix)
        cols = len(matrix[0])

        maxsqlen = 0
        for i in range(rows):
            for j in range(cols):
                # initilize flag to be True
                if matrix[i][j] == ‘1‘:
                    sqlen = 1
                    flag = True
                    # border limits
                    while sqlen + i < rows and sqlen + j < cols and flag:
                        # advance square length downwords
                        for k in range(j, sqlen + j + 1):
                            if matrix[i+sqlen][k] == ‘0‘:
                                flag = False
                                break
                        # advance square length to the right
                        for k in range(i, sqlen + i + 1):
                            if matrix[k][j+sqlen] == ‘0‘:
                                flag = False
                                break

                        if flag:
                            sqlen += 1

                    if maxsqlen < sqlen:
                        maxsqlen = sqlen

        return maxsqlen * maxsqlen 

Complexity Analysis

  • Time complexity : O\big((mn)^2\big)O((mn)?2??). In worst case, we need to traverse the complete matrix for every 1.
  • Space complexity : O(1)O(1). No extra space is used.

Sol 2:

DP.

(sth. wrong with initlization of DP...)

class Solution(object):
    def maximalSquare(self, matrix):
        """
        :type matrix: List[List[str]]
        :rtype: int
        """
        # DP
        # Time complexity : O(mn)O(mn). Single pass.
        # pace complexity : O(mn)O(mn). Another matrix of same size is used for dp.

        if len(matrix) <= 0 :
            return 0

        rows = len(matrix)
        cols = len(matrix[0])

        maxsqlen = 0
        dp = [‘0‘] * (rows + 1) * (cols + 1)

        for i in range(1, rows+1):
            for j in range(1, cols+1):
                if matrix[i-1][j-1] == ‘1‘:
                    dp[i][j] = min(dp[i][j-1], dp[i-1][j], dp[i-1][j-1]) + 1
                    maxsqlen = max(maxsqlen, dp[i][j])

        return maxsqlen * maxsqlen
        
时间: 2024-08-01 10:33:01

221. Maximal 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*

【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

[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]作为右下节点的时候

(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

(DP 线性DP) 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. Example: Input: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Output: 4 =============================================================== 这是一个D

[LC] 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. Example: Input: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Output: 4 class Solution { public int maximalSquare(char[][] matrix) { if (matr