Analysis:
I search from the internet. This is a really skillful solution. In case, I will forget in the future. The key parts are as followed:
- keep the stack in the incremental order.
- Compute the width maximum when the stack is empty and height maximum at every step
- Clear the stack before exit, remember that the width should be the number of the rectangles
Time Complexity:
- O(n)
Space Complexity:
- O(n)
Code
1 | class Solution(object): |