[LeetCode] Maximal Rectangle
2013-11-03 14:50
399 查看
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
这道题太难想了,比较全的总结在这里。
http://hi.baidu.com/mzry1992/item/030f9740e0475ef7dc0f6cba
核心是使用极大化思想,全局最大值肯定是局部最大值中的一个。这个题目让我瞬间联想到了高考数学里做的题目,类似于要求一个函数(全局)最大值,先用把该函数求导,然后等于0的时候就是取极值的时候,然后这些极大值里面必定会有一个是全局最大值。
不过在网上搜了下,最好理解的方法是运用LeetCode中另外一题——求直方图里最大矩形。
1)以每一行为底,然后在这一行(包括此行)之上的所有空间看作是直方图的空间,求出该直方图里的最大矩形。
2)对每一行都这样做,那么就会有若干个最大矩形的候选项。选出全局最大的一个。
因为求直方图的最优解法为O(N),所以这样可以达到最优的O(N^2)。
灵活运用已经做过的经典题目也是一种重要的解题思路哇~~~
这道题太难想了,比较全的总结在这里。
http://hi.baidu.com/mzry1992/item/030f9740e0475ef7dc0f6cba
核心是使用极大化思想,全局最大值肯定是局部最大值中的一个。这个题目让我瞬间联想到了高考数学里做的题目,类似于要求一个函数(全局)最大值,先用把该函数求导,然后等于0的时候就是取极值的时候,然后这些极大值里面必定会有一个是全局最大值。
不过在网上搜了下,最好理解的方法是运用LeetCode中另外一题——求直方图里最大矩形。
1)以每一行为底,然后在这一行(包括此行)之上的所有空间看作是直方图的空间,求出该直方图里的最大矩形。
2)对每一行都这样做,那么就会有若干个最大矩形的候选项。选出全局最大的一个。
因为求直方图的最优解法为O(N),所以这样可以达到最优的O(N^2)。
灵活运用已经做过的经典题目也是一种重要的解题思路哇~~~
public int maximalRectangle(char[][] matrix) { if (matrix.length == 0) return 0; // initialize the height that starts from each row and extends in a // bottom-up fashion // note that the # of columns is one larger because an extra 0 needs to // be appended for the given largest rectangle in histogram algorithm int[][] height = new int[matrix.length][matrix[0].length + 1]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { if (matrix[i][j] == '0') { height[i][j] = 0; } else { if (i == 0) height[i][j] = 1; else // height[i-1][j] will be 0 if matrix[i-1][j] = 0 height[i][j] = height[i - 1][j] + 1; } } } // calculate the largest rectangle in each histogram int maxArea = 0; for (int i = 0; i < matrix.length; i++) { maxArea = Math.max(maxArea, largestRectangleinHistogram(height[i])); } return maxArea; } private int largestRectangleinHistogram(int[] height) { Stack<Integer> stack = new Stack<Integer>(); int i = 0; int maxArea = 0; while (i < height.length) { if (stack.isEmpty() || height[stack.peek()] <= height[i]) { stack.push(i++); } else { int t = stack.pop(); maxArea = Math.max(maxArea, height[t] * (stack.isEmpty() ? i : i - stack.peek() - 1)); } } return maxArea; }
相关文章推荐
- leetcode Two Sum
- leetcode Median of Two Sorted Arrays
- leetcode Longest Substring Without Repeating Characters
- leetcode Add Two Numbers
- leetcode Longest Palindromic Substring
- [LeetCode] Populating Next Right Pointers in Each Node
- [LeetCode] Populating Next Right Pointers in Each Node II
- [LeetCode] Distinct Subsequences
- [LeetCode] Flatten Binary Tree to Linked List
- [LeetCode] Path Sum
- [LeetCode] Path Sum II
- [LeetCode] Minimum Depth of Binary Tree - BFS
- [LeetCode] Minimum Depth of Binary Tree - DFS
- [LeetCode] Balanced Binary Tree
- [LeetCode] Convert Sorted List to Binary Search Tree
- [LeetCode] Convert Sorted Array to Binary Search Tree
- [LeetCode] Binary Tree Level Order Traversal II
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] Maximum Depth of Binary Tree