LeetCode: Maximal Rectangle
来源:互联网 发布:网络图编辑软件 编辑:程序博客网 时间:2024/05/17 20:33
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
受mitbbs版上某楼的启发,把这道题转化为之前做过的 maximal rectangle in histogram来做。
class Solution {public: int maximalRectangle(vector<vector<char> > &matrix) { // Start typing your C/C++ solution below // DO NOT write int main() function int nSize = matrix.size(); if(nSize == 0){ return 0; } vector<vector<char> > heights; heights.push_back(vector<char>(matrix[0].size(), 0)); int max = 0; for(int i = 0; i< nSize; ++i){ vector<char> row; for(int j = 0; j< matrix[i].size(); ++j){ if(matrix[i][j] == '1'){ row.push_back(heights[i][j] + 1); } else{ row.push_back(0); } } heights.push_back(row); } for(int i = 1; i <= nSize; ++i){ int temp = largestRectangleArea(heights[i]); max = max > temp? max: temp; } return max; } int largestRectangleArea(vector<char> &height) { // Start typing your C/C++ solution below // DO NOT write int main() function int nSize = height.size(); int l[nSize + 1];//left bar position int h[nSize + 1];//height of the left bar l[0] = h[0] = 0; int cur_h = 0; int max = 0; int top = 0; for(int i = 0 ; i<= nSize; ++i){ cur_h = i==nSize? 0: height[i]; int j = i; for(; h[top] > cur_h; j = l[top--]){ int temp = (i - l[top]) * h[top]; if(temp > max){ max = temp; } } if(cur_h > h[top]){ h[++top] = cur_h; l[top] = j; } } return max; } };
mitbbs上看到的另一个解法。
《浅谈用极大化思想解决最大子矩形问题》这篇文章 里面介绍了一种悬线法的 DP 时间复杂度是 O(mn) 的 原文图文讲解很好 实现起来也比 histogram 那个容易一些。
class Solution {public: int maximalRectangle(vector<vector<char> > &matrix) { if (matrix.empty()) { return 0; } int n = matrix[0].size(); vector<int> H(n); vector<int> L(n); vector<int> R(n, n); int ret = 0; for (int i = 0; i < matrix.size(); ++i) { int left = 0, right = n; for (int j = 0; j < n; ++j) { if (matrix[i][j] == '1') { ++H[j]; L[j] = max(L[j], left); } else { left = j+1; H[j] = 0; L[j] = 0; R[j] = n; } } for (int j = n-1; j >= 0; --j) { if (matrix[i][j] == '1') { R[j] = min(R[j], right); ret = max(ret, H[j]*(R[j]-L[j])); } else { right = j; } } } return ret; }};
- 【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)
- [Leetcode] Jump Game II
- [Leetcode] Length of Last Word
- [Leetcode] Generate Parentheses
- LeetCode: Longest Valid Parentheses
- DBCP连接池的最简单应用(用于ORACLE数据库)
- LeetCode: Maximal Rectangle
- 9 Regular Expressions to strip HTML tags
- Maximum Depth of Binary Tree
- Tiny 210 Linux Embedded --编译 FFMPEG + SDL 自用笔记
- LeetCode: Maximum Subarray
- Discover Microsoft Team Foundation Server
- 循序渐进PostgreSQL FAQ之安装篇
- 两个list相加
- Tiny 210 LCD 设置笔记