Implement Stack/Queue using Queue/Stack

  Implement Stack using Queue Use Two Queues Implement a stack API including push(), pop(), top(), empty(). The pop() and top() are the tricky ones to implement. We can use two queues (q1 and q2). push(x): add x to q1; O(1) empty(): check whether q1 is empty; O(1) pop(): move all items from q1 and […]

Sliding Window Max/Median

Given an array and an sliding window size, while the window slide through the array, determine the max element within each window. Monotonic Queue Problem Maintain two deque q1 and q2. q1 represents the sliding window (remove() to remove from front and add() to add to the back if exceeds window size k). Think about […]

Remove Invalid Parentheses

Given a string with parentheses and letters. Remove the minimum number of parentheses so the string becomes valid. To check if a string is valid or not, we can maintain a counter. Walk through the string, if find a letter, move on. If find a left paren, increase the counter by 1, if find a […]