[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 adding this problem and creating all test cases.

给一个2维的01矩阵,找出最大只含有1的正方形,返回它的面积。

解法1: Brute force,对于矩阵中的每一个为1的点,都把它当作正方形的左上角,然后判断不同大小的正方形内的点是不是都为1。

解法2: DP,对于第一种的解法肯定有很多的重复计算,所以可以用DP的方法,把某一点能组成的最大正方形记录下来。

Python: DP

class Solution:
    # @param {character[][]} matrix
    # @return {integer}
    def maximalSquare(self, matrix):
        if not matrix:
            return 0

        m, n = len(matrix), len(matrix[0])
        size = [[0 for j in xrange(n)] for i in xrange(m)]
        max_size = 0

        for j in xrange(n):
            if matrix[0][j] == ‘1‘:
                size[0][j] = 1
            max_size = max(max_size, size[0][j])

        for i in xrange(1, m):
            if matrix[i][0] == ‘1‘:
                size[i][0] = 1
            else:
                size[i][0] = 0
            for j in xrange(1, n):
                if matrix[i][j] == ‘1‘:
                    size[i][j] = min(size[i][j - 1],                                       size[i - 1][j],                                       size[i - 1][j - 1]) + 1
                    max_size = max(max_size, size[i][j])
                else:
                    size[i][j] = 0

        return max_size * max_size

C++:

class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        if (matrix.empty() || matrix[0].empty()) return 0;
        int m = matrix.size(), n = matrix[0].size(), res = 0;
        vector<vector<int>> dp(m, vector<int>(n, 0));
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (i == 0 || j == 0) dp[i][j] = matrix[i][j] - ‘0‘;
                else if (matrix[i][j] == ‘1‘) {
                    dp[i][j] = min(dp[i - 1][j - 1], min(dp[i][j - 1], dp[i - 1][j])) + 1;
                }
                res = max(res, dp[i][j]);
            }
        }
        return res * res;
    }
};

C++:

class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        if (matrix.empty() || matrix[0].empty()) return 0;
        int m = matrix.size(), n = matrix[0].size(), res = 0, pre = 0;
        vector<int> dp(m + 1, 0);
        for (int j = 0; j < n; ++j) {
            for (int i = 1; i <= m; ++i) {
                int t = dp[i];
                if (matrix[i - 1][j] == ‘1‘) {
                    dp[i] = min(dp[i], min(dp[i - 1], pre)) + 1;
                    res = max(res, dp[i]);
                } else {
                    dp[i] = 0;
                }
                pre = t;
            }
        }
        return res * res;
    }
};

 

 

  

  

原文地址:https://www.cnblogs.com/lightwindy/p/8614962.html

时间: 2024-10-15 12:37:27

[LeetCode] 221. Maximal Square 最大正方形的相关文章

(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

(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

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

[虚拟机OA]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. Input: 1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0 Output: 4 (参考LeetCode 221 Maximal Square) 思路: dp[i][j] 代表在以i, j 这个点为右下角的正方形变成若这一格的值是1, 则这个正方

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

[LintCode] 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. Have you met this question in a real interview? Example 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

【Leetcode】Maximal Square

题目链接:https://leetcode.com/problems/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

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