mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-21 02:53:15 +08:00
Moved StackPostfixNotation.java from the Others section to the Stack section (#4372)
* Moved StackPostfixNotation.java from the Others section to the Stack section * Put all stack related algo in a separate stack directory in the algorithms directory. The stack directory under data-structures now only contains various implementations of the stack data structure. * formatted files
This commit is contained in:
35
src/main/java/com/thealgorithms/stacks/LargestRectangle.java
Normal file
35
src/main/java/com/thealgorithms/stacks/LargestRectangle.java
Normal file
@ -0,0 +1,35 @@
|
||||
package com.thealgorithms.stacks;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author mohd rameez github.com/rameez471
|
||||
*/
|
||||
|
||||
public class LargestRectangle {
|
||||
|
||||
public static String largestRectanglehistogram(int[] heights) {
|
||||
int n = heights.length, maxArea = 0;
|
||||
Stack<int[]> st = new Stack<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
int start = i;
|
||||
while (!st.isEmpty() && st.peek()[1] > heights[i]) {
|
||||
int[] tmp = st.pop();
|
||||
maxArea = Math.max(maxArea, tmp[1] * (i - tmp[0]));
|
||||
start = tmp[0];
|
||||
}
|
||||
st.push(new int[] {start, heights[i]});
|
||||
}
|
||||
while (!st.isEmpty()) {
|
||||
int[] tmp = st.pop();
|
||||
maxArea = Math.max(maxArea, tmp[1] * (n - tmp[0]));
|
||||
}
|
||||
return Integer.toString(maxArea);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
assert largestRectanglehistogram(new int[] {2, 1, 5, 6, 2, 3}).equals("10");
|
||||
assert largestRectanglehistogram(new int[] {2, 4}).equals("4");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user