Logic Problem in Histogram Problem

I could not understand why we used stack specifically to solve this problem.
Also could you send me the code for this logic as there are a few steps which I did not understand

see using stack provides the most optimised way of solving the question


read here which is O(N^2) approach using stack
then we move towards a more optimised method

please share the articles and you`ll understand a lot

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.