Contianer


Stack

Last in first out

empty()

size()

top()

queue uses "front()".

push()

pop()

swap()


[Hard] No.42 Trapping Rain Water

Approach: Stack

Time Complexity: O(N), where N is the number of the list.

Space Complexity: O(N), where N is worst case of the size of stack.


Parentheses related question

Concept

Whenever you encounter an OPENING bracket (LEFT side) i.e. '(', '[', '{', executer PUSH.

Whenever you encounter an CLOSING bracket (RIGHT side) i.e. ')', ']', '}', executer POP.


[Easy] No.20 Valid Parentheses

Approach: Stack

Time Complexity: O(N), where N is the length of string.

Space Complexity: O(N), where N is the length of string.

[Hard] No.726 Number of Atoms

Approach: Stack + map

Time Complexity: O(N^2), where N is the length of formula.

Space Complexity: O(N), where N is the length of formula.