LeetCode OJ: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
在给定的二维字符数组,找出最大全为1的矩形包含的1的个数:
注意这里相当于求面积而已,只要求出边长就很好办了。边长用dp很好求,这题其实跟前面有道dp问题很像,以后有时间找出来,先贴下代码:
 class Solution {
 public:
     int maximalSquare(vector<vector<char>>& matrix) {
         if(!matrix.size()||!matrix[].size()) return ;
         vector<vector<int>>dp(matrix.size(), vector<int>(matrix[].size(), ));
         int maxVal = ;
         for(int i = ; i < matrix.size(); ++i){
             if(matrix[i][] == ''){
                 dp[i][] = ;
                 maxVal = ;
             }else dp[i][] = ;
         }
         for(int j = ; j < matrix[].size(); ++j){
             if(matrix[][j] == ''){
                 dp[][j] = ;
                 maxVal = ;
             }else dp[][j] = ;
         }
         for(int i = ; i < matrix.size(); ++i){
             for(int j = ; j < matrix[].size(); ++j){
                 if(matrix[i][j] == ''){
                     dp[i][j] = min(dp[i-][j], min(dp[i][j-], dp[i-][j-])) + ;
                     maxVal = max(maxVal, dp[i][j]);
                 }else
                     dp[i][j] = ;
             }
         }
         return maxVal*maxVal;
     }
 };
LeetCode OJ:Maximal Square(最大矩形)的更多相关文章
- [LeetCode] 85. Maximal Rectangle 最大矩形
		
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
 - 求解最大正方形面积 — leetcode 221. Maximal Square
		
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
 - [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 ret ...
 - 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 ret ...
 - (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 ret ...
 - [LeetCode] 221. Maximal Square _ Medium  Tag: Dynamic Programming
		
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
 - 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 ...
 - Leetcode 221. Maximal Square
		
本题用brute force超时.可以用DP,也可以不用. dp[i][j] 代表 以(i,j)为右下角正方形的边长. class Solution(object): def maximalSquar ...
 - LeetCode OJ 之 Maximal Square (最大的正方形)
		
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
 - [LeetCode] Maximal Rectangle 最大矩形
		
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
 
随机推荐
- 两种ajax的方法
			
两种Ajax方法 Ajax是一种用于快速创建动态网页的技术,他通过在后台与服务器进行少量的数据交换,可以实现网页的异步更新,不需要像传统网页那样重新加载页面也可以做到对网页的某部分作出更新,现在这项技 ...
 - 前端学习笔记之HTML中的id,name,class区别
			
name 属性用于在 JavaScript 中对元素进行引用,或者在表单提交之后,对表单数据进行引用. html的name和id可以类比身份证的姓名和身份证编号,编号id具有唯一性,一个id只出现一次 ...
 - nginx + resin配合使用问题
			
在A机器上用resin部署了一台接口服务器,用了一段时间后需要扩容. 从A上拷贝所有配置和数据至B服务器,修改了resin的IP配置. <server id="" addre ...
 - GET 对比 POST
			
HTTP 方法:GET 对比 POST HTTP 消息 标签列表(字母排序) 两种最常用的 HTTP 方法是:GET 和 POST. 什么是 HTTP? 超文本传输协议(HTTP)的设计目的是保证客户 ...
 - 异步:asyncio和aiohttp的一些应用(1)
			
1. asyncio 1.1asyncio/await 用法 async/await 是 python3.5中新加入的特性, 将异步从原来的yield 写法中解放出来,变得更加直观. 在3.5之前,如 ...
 - Numpy 数据分析基础
			
numpy 库 ndarray : numpy 的关键 a = np.array([1,2,3]) # 轴 a.ndim # 数组长度 a.size # 数组的型 a.shape # 类型 a.dty ...
 - codeforces Round#429 (Div2)
			
2017-08-20 10:00:37 writer:pprp 用头文件#include <bits/stdc++.h>很方便 A. Generous Kefa codeforces 84 ...
 - Python学习札记(二十一) 函数式编程2 map/reduce
			
参考:map/reduce Note 1.map():map()函数接收两个参数,一个是函数,一个是Iterable.map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. ...
 - ThinkPHP开发笔记-控制器
			
1.下面就是一个典型的控制器类的定义: <?php namespace Home\Controller; use Think\Controller; class IndexController ...
 - Global.asax详解
			
在网上找了N多相关的东西总说的不够细,现在终于找到了.可以了解web.cofig和Global.asax之间的关系以及执行的顺序. 在Global.asax.cs文件中 protected void ...