[LeetCode]Maximal Rectangle
2014-01-21 09:32
190 查看
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
思考:跟上一题一样,维护左右边界。参考:http://discuss.leetcode.com/questions/260/maximal-rectangle#
思考:跟上一题一样,维护左右边界。参考:http://discuss.leetcode.com/questions/260/maximal-rectangle#
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; // calculate L(i, j) from left to right 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; } } // calculate R(i, j) from right to right 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 84] 56 Merge Intervals
- LeetCode Unique Paths
- [LeetCode] Binary Tree Level Order Traversal II
- [leetcode] Path Sum
- LeetCode-Median of Two Sorted Arrays-计算中值-有序表合并
- 【LeetCode】164. Maximum Gap (2 solutions)
- 刷leetcode:Roman to Integer/ Integer to Roman
- 3Sum Closest--LeetCode
- Maximal Square Leetcode Java
- LeetCode "Maximal Square"
- [leetcode 226] Invert Tree
- LeetCode 116: Populating Next Right Pointers in Each Node
- leetcode-56 Merge Intervals 合并区间
- 【LEETCODE】82- Remove Duplicates from Sorted List II [Python]
- leetcode 84. Largest Rectangle in Histogram
- zigzag相关题目2--LeetCode(6) ZigZag Conversion
- Leetcode 373. Find K Pairs with Smallest Sums(Python)
- 【LeetCode】126. Word Ladder II
- leetcode problem sum
- Leetcode177. Nth Highest Salary(SQL语句中使用函数)