-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestRectangle.java
More file actions
55 lines (47 loc) · 1004 Bytes
/
LargestRectangle.java
File metadata and controls
55 lines (47 loc) · 1004 Bytes
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
import java.util.*;
public class LargestRectangle {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public int largestRectangleArea(int[] height) {
LinkedList<Integer> stack = new LinkedList<Integer>();
int index = 0;
int maxArea = 0;
while(index < height.length)
{
if(stack.isEmpty() || height[index] > height[stack.peek()])
{
stack.push(index);
index++;
}
else
{
int highOne = stack.pop();
if(stack.isEmpty())
{
maxArea = Math.max(maxArea, height[highOne]*(index));
}
else
{
maxArea = Math.max(maxArea, height[highOne]*(index - stack.peek() -1));
}
}
}
while(!stack.isEmpty())
{
int highOne = stack.pop();
if(stack.isEmpty())
{
maxArea = Math.max(maxArea, height[highOne]*(height.length));
}
else
{
maxArea = Math.max(maxArea, height[highOne]*(height.length - stack.peek() -1));
}
}
return maxArea;
}
}