-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_stack_n^2.java
55 lines (47 loc) · 1.58 KB
/
AC_stack_n^2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_stack_n^2.java
* Create Date: 2015-03-08 11:10:08
* Descripton: Use 084. Largest Rectangle in Histogram (Hard)
*/
import java.util.*;
public class Solution {
// 084. Largest Rectangle in Histogram (Hard)
private int largestRectangleArea(int[] height) {
int len = height.length;
Stack<Integer> stk = new Stack<Integer>();
int ret = 0;
for (int i = 0; i <= len; ++i) {
int h = (i == len ? 0 : height[i]);
if (stk.isEmpty() || h >= height[stk.peek()]) {
stk.push(i);
} else {
int top = stk.pop();
ret = Math.max(ret, height[top] * (stk.empty() ? i : i - stk.peek() - 1));
--i; // back one step again
}
}
return ret;
}
public int maximalRectangle(char[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
int ret = 0;
int[] height = new int[matrix[0].length];
for (int i = 0; i < matrix.length; ++i) {
for (int j = 0; j < matrix[0].length; ++j) {
height[j] = matrix[i][j] == '0' ? 0 : height[j] + 1;
}
ret = Math.max(ret, largestRectangleArea(height));
}
return ret;
}
// debug
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
Solution s = new Solution();
int[] input = {1, 2, 3, 4};
System.out.println("no case");
}
}