My code:
public class Solution {
public int maximalRectangle(char[][] matrix) {
if (matrix == null || matrix.length == 0)
return 0;
int[][] tracker = new int[matrix.length][matrix[0].length];
/** initialize tracker[][] */
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == '1')
tracker[i][j] = 1;
}
}
/** calculate area from up to down without consideration of nearing area */
for (int i = 1; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (tracker[i][j] != 0) {
tracker[i][j] += tracker[i - 1][j];
}
}
}
/** find the maximum area */
int max = 0;
for (int i = 0; i < matrix.length; i++) {
int maximumArea = largestRectangleArea(tracker[i]);
max = Math.max(max, maximumArea);
}
return max;
}
/** get the maximal area in each line, using larest rectangle in histogram */
private int largestRectangleArea(int[] height) {
if (height == null || height.length == 0)
return 0;
Stack<Integer> s = new Stack<Integer>();
int[] helper = new int[height.length + 1];
for (int i = 0; i < height.length; i++)
helper[i] = height[i];
int max = 0;
int zeroLocation = -1;
for (int i = 0; i < helper.length; i++) {
if (s.isEmpty() || helper[i] >= helper[s.peek()]) { // insert into stack with increasing elem values
s.push(i);
}
else {
int end = i; // get the end index
while (!s.isEmpty()) {
if (helper[s.peek()] >= helper[i]) { // if bigger, pop, area = value * (end - begin)
int index = s.pop();
int area = 0;
if (s.isEmpty()) {
int begin = zeroLocation;
area = helper[index] * (i - begin - 1);
}
else {
int begin = s.peek();
area = helper[index] * (i - begin - 1);
}
max = Math.max(max, area); // get the biggest area
}
else
break;
}
if (helper[i] > 0)
s.push(i); // push this index into stack
else
zeroLocation = i;
}
}
return max;
}
}
這道題目之前做過,但是沒有開文章,不知道怎么沒寫。
今天寫了后思路很明確,就是疊加,然后用
- Largest Rectangle in Histogram
http://www.lxweimin.com/p/1cb8091863d5
作為輔助找到每一行(作為直方圖來看)疊加在一起的最大面積。
這是解法的鏈接:
http://www.programcreek.com/2014/05/leetcode-maximal-rectangle-java/
Anyway, Good luck, Richardo!
My code:
public class Solution {
public int maximalRectangle(char[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
int row = matrix.length;
int col = matrix[0].length;
int[][] map = new int[row][col];
for (int i = 0; i < col; i++) {
if (matrix[0][i] == '1') {
map[0][i] = 1;
}
}
for (int i = 1; i < row; i++) {
for (int j = 0; j < col; j++) {
if (matrix[i][j] == '0') {
map[i][j] = 0;
}
else {
map[i][j] = 1 + map[i - 1][j];
}
}
}
int max = 0;
for (int i = 0; i < row; i++) {
max = Math.max(max, getMaxHistogram(map[i]));
}
return max;
}
private int getMaxHistogram(int[] histogram) {
if (histogram == null || histogram.length == 0) {
return 0;
}
Stack<Integer> s = new Stack<Integer>();
int[] h = new int[histogram.length + 1];
for (int i = 0; i < histogram.length; i++) {
h[i] = histogram[i];
}
int max = 0;
for (int i = 0; i < h.length; i++) {
if (s.isEmpty() || h[i] >= h[s.peek()]) {
s.push(i);
}
else {
while (!s.isEmpty() && h[i] < h[s.peek()]) {
int high = h[s.pop()];
int end = i;
int begin = (s.isEmpty() ? 0 : s.peek() + 1);
int area = high * (end - begin);
max = Math.max(max, area);
}
s.push(i);
}
}
return max;
}
}
差不多的想法, 用直方圖最大面積那道題目作為基礎。
DP solution:
My code:
public class Solution {
public int maximalRectangle(char[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
int row = matrix.length;
int col = matrix[0].length;
int[][] dp = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = col - 1; j >= 0; j--) {
if (matrix[i][j] == '1') {
dp[i][j] = 1;
if (j < col - 1) {
dp[i][j] += dp[i][j + 1];
}
}
}
}
int max = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (max >= (row - i) * (col - j)) { // assume all 1 in this area, it is still smaller than max, then skip
continue;
}
else {
int min = Integer.MAX_VALUE;
for (int k = i; k < row; k++) {
if (dp[k][j] == 0) {
break;
}
min = Math.min(min, dp[k][j]);
int area = (k - i + 1) * min;
max = Math.max(max, area);
}
}
}
}
return max;
}
}
reference:
http://www.cnblogs.com/TenosDoIt/p/3454877.html
DP的做法真的想不出來。。。
分析為什么想不出來。
因為受了那個求最大正方形面積題目的影響。
那個是從右下角開始,一步步累加出最優解。
但是這個題目,矩形是不可能累加的,因為我們還需要考慮到形狀,朝向的問題。
那么,最直接的想法,從每個點開始算。
比如,計算以 (i, j) 為左上角的矩形的最大面積。
這個我們是可以做出來的。也就是,從第一行開始算。算出一個面積。
然后算第二行,第二行的寬必須是第一行和第二行寬的最小值。
然后算第三行。。。
這樣下去,對于一個點,復雜度是 O(n ^ 2)
那么對于整體,復雜度就是 O(n ^ 4)
這就是brute-force解法。
然后想想有沒有優化。那就是求寬度的時候,有些數據是可以cache的。
于是就可以用DP了,從右往左開始計算。
然后一切就順理成章了。
然后加入一個剪枝,沒想到效果這么好。。。
加入DP后,算法復雜度變成了 O(n ^ 3)
上面的使用直方圖的解法,算法復雜度是 O(n ^ 2)
但是DP解法加入剪枝后,速度快了很多。
Anyway, Good luck, Richardo! -- 08/18/2016