LeetCode85 Maximal Rectangle
問題來源 LeetCode85
問題描述
Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area.
For example, given the following matrix:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 6.
問題分析
這道題目其實和LeetCode84有很大的關係。比如題目給定的矩陣。
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
可以轉化成
1 0 1 0 0
2 0 2 1 1
3 1 3 2 2
4 0 0 3 0
也就是把每列連續的1 加起來,形成新的列,而矩形的面積可以通過Rectangle in Histogram的規則計算出來。
比如第3行的 3 2 2 就是給定矩陣的最大矩形,也就是2 +2 +2 =6。
所以解決這道題的方法就是先把給矩陣的每一行轉化,然後利用84題的算法進行計算,並保存最大值。
代碼如下
public int maximalRectangle(char[][] matrix) {
if(matrix==null|| matrix.length<1 ||matrix[0].length<1)
return 0;
int m =matrix.length;
int n = matrix[0].length;
for(int i=1;i<m;i++){
for (int j = 0; j < n; j++) {
if(matrix[i][j]!='0'){
matrix[i][j]=(char)(matrix[i][j]+matrix[i-1][j]-'0');
}
}
}
int max =0;
for (int i = 0; i < m; i++) {
max =Math.max(max,largestRectangleArea(matrix[i]));
}
return max;
}
public int largestRectangleArea(char[] heights) {
if(heights==null ||heights.length<1){
return 0;
}
int result = 0;
Stack<Integer> stack = new Stack<>();
stack.add(-1);
for(int i=0;i<heights.length;i++){
while(stack.size()!=1 && heights[stack.peek()]>=heights[i]){
result = Math.max((heights[stack.pop()]-'0')*(i-stack.peek()-1),result);
}
stack.add(i);
}
int right = stack.peek();
while (stack.size()>1){
result = Math.max((heights[stack.pop()]-'0')*(right-(stack.peek())),result);
}
return result;
}
LeetCode學習筆記持續更新
GitHub地址 https://github.com/yanqinghe/leetcode
CSDN博客地址 http://blog.csdn.net/yanqinghe123/article/category/7176678