博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]Maximal Rectangle
阅读量:4150 次
发布时间:2019-05-25

本文共 1788 字,大约阅读时间需要 5 分钟。

class Solution {//for each element in matrix, we compute h[j](current consecutive length of '1'), L[j] (nearest left wall whose height is smaller than current column), and R[j] (nearest right wall whose height is smaller than current column) for it, update these three terms row by row//here we compute the most far range(both in height, and width) of scan line j at every row i//to get the size of the rectangle of f[i][j]. To achieve this, we should compute://H[j](the maximum bottom up height of current column until meet the first '0')//L[j](the most far position where can the scan line j can go at the left)//R[j](the most far position where can the scan line j can go at the right)//then f[i][j]=H[j]*(R[j]-L[j]+1), ans=max(ans,f[i][j])public:	int maximalRectangle(vector
> &matrix) { // Start typing your C/C++ solution below // DO NOT write int main() function int ans = 0; if(matrix.size() == 0) return ans; int m = matrix[0].size(); vector
L(m, -1); vector
R(m, m); vector
H(m, 0); for (int i = 0; i < matrix.size(); ++i) { //scan from left to right to update H and L int nearestLeft = -1;//virtual '0' at most far of right for (int j = 0; j < matrix[i].size(); ++j) { L[j] = max(nearestLeft, L[j]); if (matrix[i][j] == '1') H[j]++; else { H[j] = 0; L[j] = -1;//note here nearestLeft = j; } } //scan from right to left to update R and calculate f[i][j] int nearestRight = m;//virtual '0' at most far of left for (int j = matrix[i].size()-1; j >= 0; --j) { R[j] = min(nearestRight, R[j]); if (matrix[i][j] == '0') { nearestRight = j; R[j] = m;//note here } //calculate f[i][j] ans = max( ans, H[j]*(R[j]-L[j]-1) ); } } return ans; }};

转载地址:http://xlxti.baihongyu.com/

你可能感兴趣的文章
【Python】学习笔记——-7.3、继承和多态
查看>>
【Python】学习笔记——-7.5、实例属性和类属性
查看>>
git中文安装教程
查看>>
虚拟机 CentOS7/RedHat7/OracleLinux7 配置静态IP地址 Ping 物理机和互联网
查看>>
Jackson Tree Model Example
查看>>
常用js收集
查看>>
如何防止sql注入
查看>>
springmvc传值
查看>>
在Eclipse中查看Android源码
查看>>
Android使用webservice客户端实例
查看>>
[转]C语言printf
查看>>
C 语言学习 --设置文本框内容及进制转换
查看>>
C 语言 学习---判断文本框取得的数是否是整数
查看>>
C 语言 学习---ComboBox相关、简单计算器
查看>>
C 语言 学习---ComboBox相关、简易“假”管理系统
查看>>
C 语言 学习---回调、时间定时更新程序
查看>>
C 语言 学习---复选框及列表框的使用
查看>>
第十一章 - 直接内存
查看>>
JDBC核心技术 - 上篇
查看>>
一篇搞懂Java反射机制
查看>>