Maximal Rectangle
2015-09-08 15:21
369 查看
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
Solution:
Solution:
class Solution { public: int maximalRectangle(vector<vector<char>>& matrix) { int m = matrix.size(); if(!m) return 0; int n = matrix[0].size(); if(!n) return 0; vector<int> height(n + 1, 0); stack<int> st; int maxArea = 0; for(int i = 0; i < m; ++i) { while(!st.empty()) st.pop(); for(int j = 0; j < n; ++j) height[j] = matrix[i][j] == '0' ? 0 : height[j] + 1; for(int j = 0; j <= n; ++j) { if(st.empty() || height[j] >= height[st.top()]) st.push(j); else { int temp = st.top(); st.pop(); maxArea = max(maxArea, height[temp] * (st.empty() ? j : j - st.top() - 1)); j--; } } } return maxArea; } };
相关文章推荐
- Aspose.words编程指南之DocumentBuilder二
- Android系统启动流程
- oracle_load_csv
- Oracle ASCII编码 转字符串
- 玩转html5(四)----使用canvas画一个时钟
- Android 创建线程执行任务
- maven 自动下载jar文件
- ABAP常用系统变量
- CURL模拟post请求
- Hadoop安装配置
- 73.iOS UIWebView重定向问题处理 RNCachingURLProtocol做缓存出现的重定向问题
- 移动H5前端性能优化指南
- StringJDBC更改数据库的两种方式
- 微信公众帐号开发教程第15篇-自定义菜单的view类型(访问网页)
- leetCode #96 Unique Binary Search Trees
- Database之六 sql面试题整理
- eclipse安装反编译插件
- C#委托
- //dl-ssl.google.com/android/repository/repository-8.xml, reason: HttpHostConnect Connection to https
- Linux Top 命令解析 比较详细