AC clean Java solution using stack
public int largestRectangleArea(int[] h) {\n      int n = h.length, i = 0, max = 0;\n        \n      Stack<Integer> s = new Stack<>();\n        \n      while (i
    public int largestRectangleArea(int[] h) {\n      int n = h.length, i = 0, max = 0;\n        \n      Stack<Integer> s = new Stack<>();\n        \n      while (i < n) {\n        // as long as the current bar is shorter than the last one in the stack\n        // we keep popping out the stack and calculate the area based on\n        // the popped bar\n        while (!s.isEmpty() && h[i] < h[s.peek()]) {\n          // tricky part is how to handle the index of the left bound\n          max = Math.max(max, h[s.pop()] * (i - (s.isEmpty() ? 0 : s.peek() + 1)));\n        }\n        // put current bar's index to the stack\n        s.push(i++);\n      }\n        \n      // finally pop out any bar left in the stack and calculate the area based on it\n      while (!s.isEmpty()) {\n        max = Math.max(max, h[s.pop()] * (n - (s.isEmpty() ? 0 : s.peek() + 1)));\n      }\n        \n      return max;\n    }
['Java']