Leetcode -- Maximal Rectangle
2015-10-27 10:56
120 查看
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
分析:
本题求解时借助了对直方图求最大矩形的解法。对每层生成一个累计直方图并求解。
分析:
本题求解时借助了对直方图求最大矩形的解法。对每层生成一个累计直方图并求解。
class Solution { public: int maxrec(vector<int> hist) { hist.push_back(0); stack<pair<int,int>> sta; int res; for(int i=0;i<hist.size();) { if(sta.empty()||sta.top().second<hist[i]) { sta.push(make_pair(i,hist[i])); i++; } else { int j=sta.top().first,h = sta.top().second; sta.pop(); int last; if(sta.empty()) last = -1; else last = sta.top().first; res = max(res,(i-last-1)*h); } } return res; } int maximalRectangle(vector<vector<char>>& matrix) { int n=matrix.size(); if(n==0) return 0; int m=matrix[0].size(); vector<int> hist(m,0); int mval=0; for(int i=0;i<n;++i) { for(int j=0;j<m;++j) { if(matrix[i][j]=='0') hist[j]=0; else hist[j]++; } mval = max(mval,maxrec(hist)); } return mval; } };
相关文章推荐
- 一些第三方框架
- 海量数据挖掘MMDS week6: 支持向量机Support-Vector Machines,SVM
- JS实现浏览器状态栏文字闪烁效果的方法
- 七里香
- CAN总线基础知识(二)
- 海量数据挖掘MMDS week6: 支持向量机Support-Vector Machines,SVM
- Java基础--网络编程
- STM32F103ZET+DM9051--SPI+以太网+web
- 使用httpclient4.3.2来实现微信临时素材的上传
- 一些概念
- Android异常信息统计
- nginx禁止访问.git文件的设置教程(nginx过滤.git文件夹)
- SQLServer 网络协议(一)
- ASP.NET AJAX应用
- Error: unrecognized flag -version
- CCPC - k题(dp)
- 在windows上部署使用Redis
- 在div里实现它的一个子div定宽,而另一个子div获取剩下的宽度
- java1.8--改进的接口
- 3d max 视图快捷键