python leetcode 日记--Maximal Square--221

题目:

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.

本题为一个典型的动态规划问题,因此可以使用动态规划的思想进行。

step1,初始化原始行和列

step2,构建存储结果矩阵(其大小与原始问题空间一样)

step3,对结果矩阵进行遍历找出最优解

其中,step2是动态规划的核心,其主要思想为将整个问题划分为若干个子问题,对于每个子问题,都可以借助其他子问题的解来得到自己的解;

那么对应到问题中核心思想如何理解呢?

1 1 1 1
1 1 0 1
0 1 0 1
1 1 1 1

如上这个4*4表格,首先我们假定是有当前位置相左上方画正方形,那么(0,0)位置开始,初始化时仅看第一行第一列,因为是边界,因此如果值等于1,那么表示可以画出一个1单位长度的正方形满足题意。若等于0,则表示没有满足的正方形。根据此原则,结果矩阵第一行为(1,1,1,1)第一列为(1,1,0,1),然后从(1,1)位置开始遍历剩余的位置。那么怎么划分子问题呢?其实划分子问题已经开始了,比如现在在(1,1)位置,因为此位置已经为1,如果(1,1)左上方的三个位置都为的话,那么对于(1,1)这个位置的对应的结果矩阵就可以为2,而要知道剩下三个位置是否为1,只需要查那三个位置的结果矩阵即可,因为结果矩阵纪录了能够画出多少个单位长度的正方形,根据这个原理,只要找到三个中的最小值,在加上自己的这个位置,就可以得到结果。这样遍历下来,结果矩阵就纪录了从此点向左上方画正方形所满足题意的最大值

本人的实现代码如下:

class Solution(object):
    def maximalSquare(self, matrix):
        if not matrix:
            return 0
        lines=len(matrix)
        lists=len(matrix[0])
        mat=[[0]*lists for i in range(lines)]#初始化
        for i in range(lists):
            mat[0][i]=int(matrix[0][i])
        for i in xrange(lines):
            mat[i][0]=int(matrix[i][0])
        result=0#构造结果矩阵
        for i in xrange(1,lines):
            for j in xrange(1,lists):
                mat[i][j]=int(matrix[i][j])
                if mat[i][j] is not 0:
                    mat[i][j]=(min(mat[i-1][j-1],mat[i][j-1],mat[i-1][j])+1)
        result=0#遍历结果矩阵得到结果
        for i in mat:
            for j in i:
                if result<j:
                    result=j
        return  result**2
时间: 2024-08-07 14:37:33

python leetcode 日记--Maximal Square--221的相关文章

python leetcode 日记 --Contains Duplicate II --219

题目: Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and jis at most k. 给定一个整形数组和一个整数型数k,找出在这个数组中是否存在两个相同的数,并且这两个数的下标的距离小于k. "&q

【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

leetCode(27):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. 动态规划 class Solution { public: int maximalSqua

python leetcode 日记--231. Power of Two

题目: Given an integer, write a function to determine if it is a power of two. class Solution(object): def isPowerOfTwo(self, n): # """ :type n: int :rtype: bool """ 方法:分析2的幂次方的特点,发现2的任意次方的数,化成二进制时只有首位为1其余位为0,因此我的解决方法如下: class

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

【动态规划】leetcode - 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. 分析: 利用动态规划求解.建立一个类node.nod

[虚拟机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, 则这个正方

[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