| Problem Names | Solution Writeups | Code |
|---|---|---|
| Valid Parantheses |
Create stack to check, take note of closing brackets that come first in the string and also single bracket case. |
|
| Evaluate Reverse Polish Notation |
Create stack, push only numbers to stack, when encountering operator, pop from stack twice, perform operation and then push the ans back to the stack. |
|
| Generate Parantheses |
(For C++) Create deque, push_back and pop_front to generate parantheses, do backtrack to generate all possibilities. The rule is open bracket count >= close_bracket_count |
|
| Daily Temperatures |
Create priority queue that keep track of temperatures and will update days needed to wait when a new temperature from a future day is detected. |
|
| Car Fleet |
Create vector of pair(position, time). Two cars form a cluster if time_prev - time_next <=0. Add cluster count if two adjacent cars not colliding. |
|
| Min Stack |
Create a stack of pair of int, first integer stores the value, and second integer stores the current min when the value is pushed to the stack. |
|
| Largest Rectangle in Histrogram |
Create a stack that stores curr_idx and heights[i]. If two consecutive heights are in increasing order, simply push and check the next height. If two consecutive heights are in descending order, pop all heights that are smaller than heights[i], and also compute their largest possible rectangle. Pop until no more heights in stack is less than heights[i]. We then push heights[i] and the last popped index to the stack. Once we scan through the heights array, we need to check if there exists maximum area of rectangle in the stack before we output the answer. This solution takes O(n)! |