publicclassSolution{ publicintmaximalRectangle(char[][] matrix){ if (matrix == null || matrix.length == 0) return0; int largestRectangle = 0; int[] height = newint[matrix[0].length]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { int h = matrix[i][j] - '0'; height[j] = h == 0 ? 0 : height[j] + 1; } largestRectangle = Math.max(largestRectangle, largestRectangleArea(height)); } return largestRectangle; }
privateintlargestRectangleArea(int[] height){ Stack<Integer> stack = new Stack<Integer>(); int index = 0, largestArea = 0; while (index < height.length) { if (stack.isEmpty() || height[stack.peek()] < height[index]) { stack.push(index++); } else { int h = height[stack.pop()]; int w = stack.isEmpty() ? index : index - stack.peek() - 1; largestArea = Math.max(largestArea, h * w); } } while (!stack.isEmpty()) { int h = height[stack.pop()]; int w = stack.isEmpty() ? height.length : height.length - stack.peek() - 1; largestArea = Math.max(largestArea, h * w); } return largestArea; } }