public static int largestRectangleArea(int[] arr) {

    Stack<Integer> stack = new Stack<Integer>();
    int max = 0;

    for (int curt : arr) {

      if (stack.isEmpty() || stack.peek() <= curt) {
        stack.push(curt);

      } else {

        int count = 1;
        while (!stack.isEmpty() && curt < stack.peek()) {
          // stack.pop();
          max = Math.max(max, count * stack.pop());
          count++;

        }

        for (int i = 1 ; i <= count ; i++) {
          stack.push(curt);
        }

      }
    }

    int len = 1;
    while (!stack.isEmpty()) {
      max = Math.max(max, len * stack.pop());
      len++;
    }

    return max;
  }