标签:pop lse empty new 合格 int contain code private
class Solution { public int maximalRectangle(char[][] matrix) { // invalid input. if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { return 0; } int ans = 0; int[] heights = new int[matrix[0].length]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { if (matrix[i][j] == ‘0‘) { heights[j] = 0; } else { heights[j]++; } } ans = Math.max(ans, maxRecInHistogram(heights)); } return ans; } private int maxRecInHistogram(int[] heights) { int ans = 0; Stack<Integer> stack = new Stack<>(); for (int i = 0; i <= heights.length; i++) { while (!stack.isEmpty() && (i == heights.length || heights[i] < heights[stack.peek()])) { int height = heights[stack.pop()]; int width = stack.isEmpty() ? i : i - stack.peek() - 1; ans = Math.max(ans, height * width); } stack.push(i); } return ans; } }
leetcode85 - Maximal Rectangle - hard
标签:pop lse empty new 合格 int contain code private
原文地址:https://www.cnblogs.com/jasminemzy/p/9770341.html