LeetCode-Maximal Rectangle
来源:互联网 发布:labview 算编程吗 编辑:程序博客网 时间:2024/06/07 02:02
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
Solution:
Code:
<span style="font-size:14px;">class Solution {public: void convert(vector<vector<char> > &matrix, vector<vector<int> > &heights, const int &rows, const int &cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { for (int k = i; k >= 0; k--) { if (matrix[k][j] == '0') break; else heights[i][j]++; } } } } int helper(vector<vector<int> > &heights, const int &row, const int &cols) { int result = 0; stack<int> stk; for (int col = 0; col < cols;) { if (stk.empty() || heights[row][stk.top()] <= heights[row][col]) stk.push(col++); else { int position = stk.top(); stk.pop(); result = max(result, heights[row][position]*(stk.empty()?col:(col-stk.top()-1))); } } while (!stk.empty()) { int position = stk.top(); stk.pop(); result = max(result, heights[row][position]*(stk.empty()?cols:(cols-stk.top()-1))); } return result; } int maximalRectangle(vector<vector<char> > &matrix) { int rows = matrix.size(); if (rows == 0) return 0; int cols = matrix[0].size(); vector<vector<int> > heights(rows, vector<int>(cols, 0)); convert(matrix, heights, rows, cols); int result = 0; for (int row = 0; row < rows; row++) result = max(result, helper(heights, row, cols)); return result; }};</span>
0 0
- 【LeetCode】Maximal Rectangle && Maximal Square
- Leetcode:Maximal Square & Maximal Rectangle
- LeetCode: Maximal Rectangle
- LeetCode Maximal Rectangle
- LeetCode: Maximal Rectangle
- [LeetCode] Maximal Rectangle
- [leetcode] Maximal Rectangle
- [LeetCode]Maximal Rectangle
- [leetcode]Maximal Rectangle
- LeetCode-Maximal Rectangle
- [leetcode] Maximal Rectangle
- Leetcode Maximal Rectangle
- [LeetCode] Maximal Rectangle
- leetcode Maximal Rectangle
- LeetCode Maximal Rectangle
- LeetCode | Maximal Rectangle
- [LeetCode] Maximal Rectangle
- [Leetcode] Maximal Rectangle (Java)
- oracle数据库导入导出命令!
- vs2010快捷键大全
- UVA 11971 - Polygon(概率+几何概型)
- Google面试题 数组中第K小的数字
- malformed or corrupted AST file:"Unable to load module
- LeetCode-Maximal Rectangle
- Hadoop源码编译,hadoop-2.4.1+Lubuntu(Ubuntu)
- iOS沙盒机制
- ASP.net 用EasyUI datagrid对数据操作
- [ORA-01450] maximum key length (3215) exceeded
- linux路由功能
- MySQL常用用户管理
- oracle 11g gateway 连接sql server 2000
- UVA 784 Maze Exploration