Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ajinkyajawale14499/cc8893e58d584a42ca93b8a2f8d04b98 to your computer and use it in GitHub Desktop.
Save ajinkyajawale14499/cc8893e58d584a42ca93b8a2f8d04b98 to your computer and use it in GitHub Desktop.
Largest Histogram stack improvised solution
public class Solution {
public int largestRectangleArea(int[] height) {
int len = height.length;
Stack<Integer> s = new Stack<Integer>();
int maxArea = 0;
for(int i = 0; i <= len; i++){
int h = (i == len ? 0 : height[i]);
if(s.isEmpty() || h >= height[s.peek()]){
s.push(i);
}else{
int tp = s.pop();
maxArea = Math.max(maxArea, height[tp] * (s.isEmpty() ? i : i - 1 - s.peek()));
i--;
}
}
return maxArea;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment