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

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

Maximal Rectangle

 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.

 

这题的思路完全来源,不过要对原始数据做一个处理,把每行处理成直方图形式的数据。然后调用直方图的算法就可以了,所以你的先明白直方图最大面积计算方法。时间复杂度O(n^2)。

输入的矩阵

转换后的矩阵

1 class Solution { 2 public: 3     int largestRectangleArea(vector
& height) { 4 int result=0; 5 height.push_back(0); 6 stack
mystack; 7 for(int i=0;i
>& matrix) {26 if(matrix.size()==0 || matrix[0].size()==0) return 0;27 int result=0;28 vector
> histogram;29 int rows = matrix.size();30 int cols = matrix[0].size();31 vector
height_row(cols,0);32 for(int i=0;i
result?maxArea:result;51 }52 return result;53 }54 };

 其实不需要把整个直方图矩阵得到后再计算,得到每行的直方图举证就可以计算了。

1 class Solution { 2 public: 3     int largestRectangleArea(vector
& height) { 4 int result=0; 5 height.push_back(0); 6 stack
mystack; 7 for(int i=0;i
>& matrix) {26 if(matrix.size()==0 || matrix[0].size()==0) return 0;27 int result=0;28 int rows = matrix.size();29 int cols = matrix[0].size();30 vector
height_row(cols,0);31 for(int i=0;i
result?maxArea:result;46 }47 return result;48 }49 };

 

转载于:https://www.cnblogs.com/Sean-le/p/4752827.html

你可能感兴趣的文章
快速排序
查看>>
新浪微博注册器 采用httpwebrequest请求
查看>>
NYOJ260数数小木块
查看>>
Android 数据存储
查看>>
CTreeCtl的使用
查看>>
不错的网站链接
查看>>
POJ 1742
查看>>
post方法
查看>>
21、ActionBar & Notification
查看>>
步步为营:Asp.net 通用数据容器的缺陷
查看>>
判断整除(动态规划,递推)
查看>>
题解 P1004 【方格取数】
查看>>
【OCP-052】052最新考试题库分析整理-第7题
查看>>
vuex相关(actions和mutation的异曲同工)
查看>>
Linux常用命令总结
查看>>
即时通讯软件的发展演变
查看>>
java基础总结
查看>>
算法复杂度
查看>>
Jsonlib 属性过滤器
查看>>
List 去重
查看>>