Javascript using stack (Detail breakdown)
Do I explain this good enough?``\njs\n/**\n * @param {number[]} heights\n * @return {number}\n */\nconst largestRectangleArea = function (heights) {\n    let ma
Do I explain this good enough?``\n```js\n/**\n * @param {number[]} heights\n * @return {number}\n */\nconst largestRectangleArea = function (heights) {\n    let maxArea = 0;\n    const stack = [];\n    // Append shadow rectangle (height = 0) to both side\n    heights = [0].concat(heights).concat([0]);\n    for (let i = 0; i < heights.length; i++) {\n        /**\n         *  Check if height of last element in the stack bigger than height at current position\n         *  Position in stack always smaller than current position at least 1\n         *  For example we have heights array [2,5,6,2]\n         *   1: Add shadow rectangle to array => [0,2,5,6,2,0]\n         *   2:\n         *      Expression: heights[stack[stack.length - 1]] > heights[i]\n         *      i = 0 => stack = [0],\n         *      i = 1 => heights[0] > heights[1] (0 > 2) => false => stack = [0,1]\n         *      i = 2 => heights[1] > heights[2] (2 > 5) => false => stack = [0,1,2]\n         *      i = 3 => heights[2] > heights[3] (5 > 6) => false => stack = [0,1,2,3]\n         *      i = 4 => heights[3] > heights[4] (6 > 2) => true\n         *   3: Because the previous height bigger than the current height (6 > 2) so that we can confirm that the next rectangle\n         *      will only shrink area. Let calculate the result\n         *      First we get the position from the stack and assign to variable "j" from original stack = [0,1,2,3]\n         *      Expression: (i - stack[stack.length - 1] - 1) * heights[j]\n         *       j = 3 => stack = [0,1,2] => (i - stack[3 - 1] - 1) *  => (4 - 2 - 1) * 6 = 6\n         *      Explain:\n         *      - We need to calculate area backward so that we take i - last stack (4 - 2) and because we already\n         *      get a value from stack before so that we need to subtract 1 => (4 - 2 - 1)\n         *      - The latest position in stack is the maximum height we can get (j = 3 => heights[3] = 6)\n         *       => Area calculate equal (4 - 2 - 1) * 6 = 6 (1 bar)\n         *      - Compare with the current max area (currently 0) => New max area = 6\n         *\n         *      Expression: heights[stack[stack.length - 1]] > heights[i] | stack = [0,1,2]\n         *      heights[2] > heights[4] (5 > 2) => true\n         *      j = 2 => stack = [0, 1] => (i - stack[2 - 1] - 1) * 5 => (4 - 1 - 1) * 5 = 10\n         *      Area > MaxArea (10 > 6) => New max area = 10 (2 bar)\n         *      Explain: Because the height in stack are sorted (We stop add position to stack if the next height is lower)\n         *\n         *      heights[1] > heights[4] (2 > 2) => false | stack = [0,1,2]\n         *      Keep running and the final answer it is the max area you need\n         */\n        while (stack && heights[stack[stack.length - 1]] > heights[i]) {\n            const j = stack.pop();\n            maxArea = Math.max((i - stack[stack.length - 1] - 1) * heights[j], maxArea);\n        }\n        stack.push(i);\n    }\n    return maxArea;\n};\n```
['JavaScript']