Data Structure is a way to organize data. It provides some methods to handle data stream, e.g. insert, delete, etc.
Linear Data Structure
Queue & Stack
Min Stack Use two stacks, one is storing the input, when calling pop() or peek(), pop from another stack, which stores the minimum values from top to bottom.
Implement Queue by stacks Use two stacks, one is for storing elements. When calling pop() or top(), pop the elements from the first stack and push them into the second one.
Largest Rectangle in Histogram Brute force: totally O(n^2) windows and O(n) to find the minimun one in each window, so we need O(n^3) time. Improve: for each number, search both side of it until find two smaller number, calculate the result, O(n^2) cost. Best: use a stack to store the index (all increased heights), when faced with a smaller one, pop out from tha stack and calculate the area until the value bigger than the peek() one. This method cost O(n) time, with O(n) space in worst case (two passes).