leetcode: Maximal Rectangle
来源:互联网 发布:天猫美工 编辑:程序博客网 时间:2024/06/10 14:18
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
两个矩阵,一个存行最大连续1,一个存列最大连续1
然后利用这两个矩阵计算,行矩阵值代表最大长度;找到列最大矩阵,在最大长度范围内找打最小高度,相乘得到面积
class Solution {public: int maximalRectangle(vector<vector<char> > &matrix) { if( matrix.size() == 0) return 0; const int m = matrix.size(); const int n = matrix[0].size(); vector< vector< int> > row( m + 1, vector< int>( n + 1, 0)); vector< vector< int> > col( m + 1, vector< int>( n + 1, 0)); vector< vector< int> > rectangle( m + 1, vector< int>( n + 1, 0)); for( int i = 1; i <= m; ++i){ for( int j = 1; j <= n; ++j){ if( matrix[i-1][j-1] == '1'){ row[i][j] = row[i][j-1] + 1; col[i][j] = col[i-1][j] + 1; } else{ row[i][j] = 0; col[i][j] = 0; } } } for( int i = 1; i <= m; ++i){ for( int j = 1; j <= n; ++j){ int height = col[i][j]; int length = row[i][j]; int min_height = height; int area = 0; for( int k = 1; k <= length; ++k){ min_height = min( min_height, col[i][j-k+1]);//向前回退 area = max( area, min_height * k);//这里乘以k } rectangle[i][j] = area; } } int area = 0; for( int i = 0; i < rectangle.size(); ++i) for( int j = 0; j < rectangle[i].size(); ++j) area = area > rectangle[i][j] ? area : rectangle[i][j]; return area; }};
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)
- java transient 串行化
- javascript的prototype经典使用场景
- LeetCode: Max Points on a Line [149]
- 全栈JavaScript之路(七)学习 Comment 类型节点.
- ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot exe
- leetcode: Maximal Rectangle
- 如何给ExecutorService异步计算设置超时
- UVA12716 GCD XOR 数论数学构造
- 编程题目:PAT 1007. 素数对猜想 (20)
- 一个新的开始
- 7月风怒
- QEMU-KVM创建虚拟机自动指定IP的配置
- LeetCode: Evaluate Reverse Polish Notation [150]
- 内核驱动中常见的miscdevice、platform_device、platform_driver