85:Maximal Rectangle【数组】【哈希】【栈】【DP】
来源:互联网 发布:淘宝商品编辑软件 编辑:程序博客网 时间:2024/06/06 11:00
题目链接:click~
/*题意:给出包含0和1的矩阵,从图中找出最大包含1的矩形的面积(全为1)*//** *思路: 可转化成求“最大矩形面积” * 将每一列的连续的1想象成柱子,扫描每一列,记录以当前元素 * 为柱子底端时柱子的高度。 * 扫描每一行,求以当前行,为所有柱子底端时,求最大矩形面积 */class Solution {public: int maximalRectangle(vector<vector<char> > &matrix) { int n = matrix.size(); if(n == 0) return 0; int m = matrix[0].size(); vector<vector<int>> height(n, vector<int>(m)); //记录1的高度 for(int i = 0; i < m; i ++) { int h = 0; for(int j = 0; j < n; j ++) { if(matrix[j][i] == '1') h++; else h = 0; //不连续,高度置为0 height[j][i] = h; } } //同求“最大矩形面积” int maxx = 0; for(int i = 0; i < n; i ++) { stack<int> s; s.push(-1); for(int j = 0; j < m; j++) { int pre = s.top(); if(pre < 0 || height[i][pre] < height[i][j]) s.push(j); else { s.pop(); maxx = max(maxx, height[i][pre]*(j-s.top()-1)); j--; } } while(s.top() != -1) { int pre = s.top(); s.pop(); maxx = max(maxx, height[i][pre]*(m-s.top()-1)); } } return maxx; }};
0 0
- 85:Maximal Rectangle【数组】【哈希】【栈】【DP】
- Leetcode 85 - Maximal Rectangle(dp)
- 【Leetcode】Maximal Rectangle (DP)
- [LeetCode] Maximal Rectangle(!!!DP)
- LeetCode Maximal Rectangle(dp)
- 85. Maximal Rectangle(dp)
- LeetCode(85) Maximal Rectangle
- leetcode || 85、Maximal Rectangle
- leetcode 85:Maximal Rectangle
- leetcode 85: Maximal Rectangle
- LeetCode_OJ【85】 Maximal Rectangle
- LeetCode(85) Maximal Rectangle
- Leetcode (85) Maximal Rectangle
- leetcode 85: Maximal Rectangle
- LeetCode 85 Maximal Rectangle
- [Leetcode] #85 Maximal Rectangle
- [Leetcode 85] Maximal Rectangle
- DP (9) -- Maximal Square, Maximal Rectangle,House Robber II
- 掌握 Linux 调试技术
- sql层级数据查询
- -D_REENTRANT 宏作用
- Properties属性文件载入、读取工具类
- perl 基本语法介绍
- 85:Maximal Rectangle【数组】【哈希】【栈】【DP】
- AIX网卡聚合的三种模式
- 线程同步----信号量(Semaphore)
- 脏读,不可重复读,幻读
- 项目中使用的ConfigUtil 类的封装,支持多元
- FILE文件储存
- 字符串b在字符串a中出现的次数
- 深入浅出Java垃圾回收机制
- iOS 8 Extensions