Maximal Square 我们都在寻找最高1子矩阵(leeCode)
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.
/*
* 动态规划的算法
*if(m[i][j] = 1) d[i][j] = min(d[i-1][j-1], d[i][j-1], d[i-1][j]) + 1
*m[i][j] = 0; d[i][j] = 0;
*初始化 d[0][j] = m[0][j]; d[i][0] = m[i][0];
*优化思路用一行d[j] 进行
*int preNode = d[0];
*d[0] = m[i][0];
* for(int j = 1; j < n; j++)
* {
* if(m[i][j] = 1) int temp = min(preNode, d[j-1], d[j]) + 1;
* preNode = d[j];
* d[j] = temp;
* }
*错误1:没有考虑<1,0,1,1>向量 ;把矩阵想成等宽高的
*/int min(int a, int b)
{
return a < b ? a : b;
}
int min(int a, int b, int c)
{
return min(min(a,b),min(b,c));
}
int maximalSquare(vector<vector<char>>& m) {
if(m.size() == 0) return 0;
int m_size = m.size();
// if(m_size == 1) return (m[0][0] == '0') ? 0 : 1;
int* d = new int[m[0].size()];
int max = 0;
//init
for(int i = 0; i < m[0].size(); i++)
{
d[i] = (m[0][i] == '0') ? 0 : 1;
if(d[i] > max) max = d[i];
}
//循环
for(int l = 1; l < m_size; l++) //从第1行開始
{
int preNode = d[0];
d[0] = (m[l][0] == '0') ? 0 : 1;
for(int j = 1; j < m[0].size(); j++)
{
if(m[l][j] == '0')
{
preNode = d[j];
d[j] = 0;
}
else //m[l][j] = 1时
{
int temp = min(preNode, d[j-1], d[j]);//d[l-1][j-1], d[l][j-1], d[l-1][j]
preNode = d[j];
d[j] = temp + 1;
if(d[j] > max)
{
max = d[j];
}
}
}
}
delete d;
return max*max;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
Maximal Square 我们都在寻找最高1子矩阵(leeCode)的更多相关文章
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- [LintCode] 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 - Maximal Square
称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
- 【LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- [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 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
随机推荐
- 不要打开文件,阅读Rvt信息档案
叶老师,想问一下是否能基于revit API 2014,直接在开发时传递给程序要处理的文件名称和路径.而不用再在revit软件中打开为当前活动视图,就直接获得文件里信息.这样可行不? 答: 能够.你能 ...
- poj 2513 连接火柴 字典树+欧拉通路 好题
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27134 Accepted: 7186 ...
- 如果在线显示php源代码
原文:如果在线显示php源代码 通过php提供的函数highlight_file和highlight_string实现
- JAVA进阶----ThreadPoolExecutor机制(转)
ThreadPoolExecutor机制 一.概述 1.ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线程 ...
- learning - Haskell AND Lisp vs. Haskell OR Lisp - Programmers Stack Exchange
learning - Haskell AND Lisp vs. Haskell OR Lisp - Programmers Stack Exchange Haskell AND Lisp vs. Ha ...
- HTML5 Canvas动画效果实现原理
在线演示 使用HTML5画布可以帮助我们高速实现简单的动画效果.基本原理例如以下: 每隔一定时间绘制图形而且清除图形,用来模拟出一个动画过程,能够使用context.clearRect(0, 0, x ...
- Nginx特点
Nginx特点:1,跨平台:Nginx 能够在大多数 Unix like OS编译执行,并且也有Windows的移植版本号.2,配置异常简单:很easy上手.配置风格跟程序开发一样,神一般的配置.3, ...
- 祖国版SoloWheel:Airwheel爱尔威火星车 拆箱&上手经验_运动户外_晒物广场_什么值得买
http://m.baidu.com/from=844b/bd_page_type=1/ssid=0/uid=3151E6C0905477A13653132D762BB6FB/pu=sz%401320 ...
- codechef Sums in a Triangle题解
Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear ...
- Python做的眼睛护士
搞了两天终于搞定了,虽然还存在一点点小问题(窗口的显示位置应该设在(0,0)).但基本可以用了. 代码分两个部分.主界面和遮挡屏幕界面.主界面设置完时间后调用遮挡屏幕界面. 1.主界面(设置 工作时间 ...