题目:

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

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

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

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  2. python leetcode 日记 --Contains Duplicate --217

    题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...

  3. leetcode之Maximal Square

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  4. python leetcode 日记--231. Power of Two

    题目: Given an integer, write a function to determine if it is a power of two. class Solution(object): ...

  5. leetcode每日解题思路 221 Maximal Square

    问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...

  6. 求解最大正方形面积 — leetcode 221. Maximal Square

    本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...

  7. 【LeetCode】221. Maximal Square

    Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...

  8. 【刷题-LeetCode】221. Maximal Square

    Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...

  9. 【动态规划】leetcode - Maximal Square

    称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...

随机推荐

  1. I7-5775C之所以被Intel跳过,是因为本身有太多BUG

    说起I7-5775C,第五代酷睿处理器,可能大多数人都没有使用过,也并不清楚他有什么样的特性. 在2015年6月份,我在日本亚马逊买了一个I7-5775C,从此噩梦就开始了(现在已经换了I7-5820 ...

  2. Angularjs 中文版API v1.3.9 阅读

    http://www.angularjsapi.cn/#/bootstrap 2016.7.4 ng.function: 1.angular.bind(self,fn,args ); 2.angula ...

  3. 开始跟踪Redis啦,开帖

    随着NoSql的流行,对这方面的产品开始关注起来,之前一直只是看看.从昨天开始决定把Redis的实现机制啃下来,毕竟代码量也就2W行. 每天花时间看看,记录下成果. here we go.

  4. [转]如何在Java中调用DLL方法

    转载地址:http://developer.51cto.com/art/200906/129773.htm Java语言本身具有跨平台性,如果通过Java调用DLL的技术方便易用,使用Java开发前台 ...

  5. 关于一个新的DOM选择器querySelector

    在传统的javascript中,提到DOM选择器,大家比较熟悉的方式是通过tag,name,id来获取,其实大家都发现如果获取比较复杂的话,用这个方法会很繁琐,这时大家应该都会想到jquery里获取一 ...

  6. zigbee学习之路(九):串口(发送)

    一.前言 今天,我们来学习和实验串口模块方面的,串口通信是我们常用的通信手段,通过串口交互,我们可以很容易的和pc机进行数据的交换和发送,所以我们今天就来学习一下.这个实验所进行的功能是一开始CC25 ...

  7. (转) CCEditBox 编辑框

    CCEditBox 编辑框 原文: http://blog.csdn.net/cloud95/article/details/8773470 分类: cocos2d-x 2013-04-08 19:1 ...

  8. EntityFramework Core 学习笔记 —— 包含与排除属性

    原文地址:https://docs.efproject.net/en/latest/modeling/included-properties.html 在模型中包含一个属性意味着 EF 拥有了这个属性 ...

  9. jquery幻灯片

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  10. javascript 设计模式2----策略模式

    1.定义:定义一系类的算法,把它们一个个封装起来,并且使它们可以相互替换 2.解释:就是把算法和一个规则单独分封,在使用时单独调用. 简单例子: var strategies = { "S& ...