85 Maximal Rectangle
public class Solution { public int maximalRectangle(char[][] matrix) { if (matrix == null || matrix.length == 0) { return 0; } int row = matrix.length; int col = matrix[0].length; int[][] rectangle = new int[row][col]; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (i == 0) { rectangle[i][j] = matrix[i][j] == '0' ? 0 : 1; } else { rectangle[i][j] = matrix[i][j] == '0' ? 0 : 1+rectangle[i - 1][j]; } } } int maxArea = 0; for (int i = 0; i < row; i++) { int tem = largestRectangleArea(rectangle[i]); maxArea = Math.max(tem, maxArea); } return maxArea; } public int largestRectangleArea(int[] height) { int i = 0; int maxArea = 0; int[] h = new int[height.length + 1]; h = Arrays.copyOf(height, height.length + 1); Stack<Integer> stack = new Stack<Integer>(); while (i < h.length) { if (stack.isEmpty() || h[stack.peek()] <= h[i]) { stack.push(i++); } else { int t = stack.pop(); maxArea = Math.max(maxArea, h[t]*(stack.isEmpty() ? i : i - stack.peek() - 1)); } } return maxArea; } }
参考:http://huntfor.iteye.com/blog/2086892
http://www.cnblogs.com/lichen782/p/leetcode_maximal_rectangle.html