fork download
  1. import java.util.*;
  2.  
  3. public class Solution {
  4. public static int largestRectangle(ArrayList < Integer > heights) {
  5.  
  6. int len = heights.size();
  7.  
  8. int rb[] = new int [len];
  9.  
  10.  
  11. Stack<Integer> st = new Stack<>();
  12.  
  13.  
  14. // calculating right boundary
  15.  
  16. for (int i = len-1; i >= 0; i--) {
  17.  
  18. int cur = heights.get(i);
  19.  
  20. while (!st.isEmpty() && heights.get(st.peek()) >= cur)
  21. st.pop();
  22.  
  23. if (st.isEmpty() == true)
  24. rb[i] = len;
  25.  
  26. else
  27. rb[i] = st.peek();
  28.  
  29. st.push(i);
  30. }
  31.  
  32.  
  33.  
  34. int lb[] = new int [len];
  35.  
  36.  
  37. st = new Stack<>();
  38.  
  39. // calculating left boundary
  40.  
  41. for (int i = 0; i < len; i++) {
  42.  
  43. int cur = heights.get(i);
  44.  
  45. while (!st.isEmpty() && heights.get(st.peek()) >= cur)
  46. st.pop();
  47.  
  48. if (st.isEmpty() == true)
  49. lb[i] = -1;
  50.  
  51. else
  52. lb[i] = st.peek();
  53.  
  54. st.push(i);
  55. }
  56.  
  57.  
  58. // calcution of max Area
  59.  
  60. int maxArea = 0;
  61.  
  62. for (int i = 0; i < len; i++) {
  63.  
  64. int width = rb[i] - lb[i] - 1;
  65.  
  66. int curArea = width * heights.get(i);
  67.  
  68. maxArea = Math.max(maxArea, curArea);
  69. }
  70.  
  71.  
  72. return maxArea;
  73. }
  74. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:3: error: class Solution is public, should be declared in a file named Solution.java
public class Solution {
       ^
1 error
stdout
Standard output is empty