Skip to content

Commit 21da0cd

Browse files
committed
rewrite
1 parent cf48cc3 commit 21da0cd

File tree

2 files changed

+39
-70
lines changed

2 files changed

+39
-70
lines changed

LargestRectangleinHistogram.java

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
1+
import java.util.Arrays;
2+
import java.util.Stack;
23

34
/**
45
* Given n non-negative integers representing the histogram's bar height where
@@ -14,35 +15,20 @@
1415
*/
1516

1617
public class LargestRectangleinHistogram {
17-
// O(n) using two stacks
1818
public int largestRectangleArea(int[] height) {
19-
int area = 0;
20-
java.util.Stack<Integer> heightStack = new java.util.Stack<Integer>();
21-
java.util.Stack<Integer> indexStack = new java.util.Stack<Integer>();
22-
for (int i = 0; i < height.length; i++) {
23-
if (heightStack.empty() || heightStack.peek() <= height[i]) {
24-
heightStack.push(height[i]);
25-
indexStack.push(i);
26-
} else if (heightStack.peek() > height[i]) {
27-
int j = 0;
28-
while (!heightStack.empty() && heightStack.peek() > height[i]) {
29-
j = indexStack.pop();
30-
int currArea = (i - j) * heightStack.pop();
31-
if (currArea > area) {
32-
area = currArea;
33-
}
34-
}
35-
heightStack.push(height[i]);
36-
indexStack.push(j);
37-
}
38-
}
39-
while (!heightStack.empty()) {
40-
int currArea = (height.length - indexStack.pop())
41-
* heightStack.pop();
42-
if (currArea > area) {
43-
area = currArea;
19+
Stack<Integer> stack = new Stack<Integer>();
20+
int i = 0;
21+
int maxArea = 0;
22+
int[] h = new int[height.length + 1];
23+
h = Arrays.copyOf(height, height.length + 1);
24+
while (i < h.length) {
25+
if (stack.isEmpty() || h[stack.peek()] <= h[i]) {
26+
stack.push(i++);
27+
} else {
28+
int t = stack.pop();
29+
maxArea = Math.max(maxArea, h[t] * (stack.isEmpty() ? i : i - stack.peek() - 1));
4430
}
4531
}
46-
return area;
32+
return maxArea;
4733
}
4834
}

MaximalRectangle.java

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,42 @@
1-
2-
31
/**
42
* Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle
53
* containing all ones and return its area.
64
*/
75

86
public class MaximalRectangle {
97
public int maximalRectangle(char[][] matrix) {
10-
int row = matrix.length;
11-
if (row == 0)
8+
int rows = matrix.length;
9+
if (rows == 0)
1210
return 0;
13-
int col = matrix[0].length;
14-
int[][] map = new int[row][col];
15-
int area = 0;
16-
for (int i = 0; i < row; i++) {
17-
for (int j = 0; j < col; j++) {
18-
int prev = 0;
19-
if (i != 0) {
20-
prev = map[i - 1][j];
21-
}
22-
if (matrix[i][j] == '1') {
23-
map[i][j] = prev + 1;
24-
} else {
25-
map[i][j] = prev;
26-
}
11+
int maxArea = 0;
12+
int cols = matrix[0].length;
13+
int[][] map = new int[rows][cols];
14+
for (int j = 0; j < cols; j++) {
15+
map[0][j] = matrix[0][j] == '0' ? 0 : 1;
16+
}
17+
for (int i = 1; i < rows; i++) {
18+
for (int j = 0; j < cols; j++) {
19+
map[i][j] = matrix[i][j] == '0' ? map[i - 1][j]
20+
: map[i - 1][j] + 1;
2721
}
2822
}
29-
for (int i = 0; i < row; i++) {
30-
for (int j = i; j < row; j++) {
31-
int[] line = new int[col];
32-
for (int k = 0; k < col; k++) {
33-
line[k] = map[j][k] - map[i][k]
34-
+ (matrix[i][k] == '0' ? 0 : 1);
23+
int[] row = new int[cols];
24+
for (int i = 0; i < rows; i++) {
25+
for (int j = i; j < rows; j++) {
26+
for (int k = 0; k < cols; k++) {
27+
row[k] = map[j][k] - (i == 0 ? 0 : map[i - 1][k]);
3528
}
36-
int l = 0;
37-
int tmp = 0;
38-
for (int f = 0; f < col; f++) {
39-
if (line[f] == j - i + 1)
40-
tmp++;
41-
else {
42-
if (tmp > l) {
43-
l = tmp;
44-
}
45-
tmp = 0;
29+
int count = 0;
30+
for (int k = 0; k < cols; k++) {
31+
if (row[k] == j - i + 1) {
32+
maxArea = Math.max(maxArea, ++count * (j - i + 1));
33+
} else {
34+
maxArea = Math.max(maxArea, count * (j - i + 1));
35+
count = 0;
4636
}
4737
}
48-
if (tmp > l) {
49-
l = tmp;
50-
}
51-
int s = (j - i + 1) * l;
52-
if (s > area) {
53-
area = s;
54-
}
5538
}
5639
}
57-
return area;
40+
return maxArea;
5841
}
5942
}

0 commit comments

Comments
 (0)