-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
53 lines (52 loc) · 1.54 KB
/
Copy pathsolution.java
File metadata and controls
53 lines (52 loc) · 1.54 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
// 84. Largest Rectangle in Histogram
// https://leetcode.com/problems/largest-rectangle-in-histogram/
// Hard | Java | Accepted 2025-11-05
// Runtime 1180 ms | Memory 92 MB
class Solution {
public int largestRectangleArea(int[] heights) {
ArrayList<Integer> nse = new ArrayList<>();
ArrayList<Integer> lse = new ArrayList<>();
for (int i = 0; i < heights.length; i++)
{
nse.add(heights.length);
lse.add(-1);
}
Stack<Integer> stack = new Stack<>();
for(int j = heights.length-1; j>=0; j--)
{
while(!stack.isEmpty() && heights[stack.peek()]>=heights[j])
{
stack.pop();
}
if(!stack.isEmpty())
{
nse.set(j, stack.peek());
}
stack.push(j);
}
stack.clear();
for(int k = 0; k<heights.length; k++)
{
while(!stack.isEmpty() && heights[stack.peek()]>=heights[k])
{
stack.pop();
}
if(!stack.isEmpty())
{
lse.set(k, stack.peek());
}
stack.push(k);
}
int max = 0;
System.out.println(nse);
System.out.println(lse);
for(int l = 0; l<heights.length; l++)
{
int start = lse.get(l);
int end = nse.get(l);
System.out.println((end-start-1));
max = Math.max(max, (end-start-1)*heights[l]);
}
return max;
}
}