forked from chuyi2007/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestRectangleInHistogram.java
More file actions
56 lines (53 loc) · 1.78 KB
/
LargestRectangleInHistogram.java
File metadata and controls
56 lines (53 loc) · 1.78 KB
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
56
public class Solution {
public int largestRectangleArea(int[] height) {
// Start typing your Java solution below
// DO NOT write main() function
int n= height.length;
Stack<Integer> st = new Stack<Integer>();
int[] areas = new int[n];
for(int i = 0; i < n; ++i){
while(!st.isEmpty() && height[i] <= height[st.peek()])
st.pop();
areas[i] = i - (st.isEmpty()?-1:st.peek());
st.push(i);
}
st.clear();
for(int i = n - 1; i >=0; --i){
while(!st.isEmpty() && height[i] <= height[st.peek()])
st.pop();
areas[i] += (st.isEmpty()?n:st.peek()) - i - 1;
st.push(i);
}
int max = 0;
for(int i = 0; i < n; ++i){
areas[i] *= height[i];
if(areas[i] > max)
max = areas[i];
}
return max;
}
public int naiveBrutalSearch(int[] height){
int[] maxes = new int[height.length];
int previous = 0;
for(int i = 0; i < height.length; i++){
if(height[i] > previous){
int max = -1;
int min = height[i];
for(int j = i; j < height.length; j++){
if(min > height[j])
min = height[j];
int area = min * (j - i + 1);
if(area > max)
max = area;
}
maxes[i] = max;
}
previous = height[i];
}
int result = 0;
for(int i = 0; i < maxes.length; i++)
if(result < maxes[i])
result = maxes[i];
return result;
}
}