You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (`push`, `peek`, `pop`, and `empty`).
6
+
7
+
Implement the `MyQueue` class:
8
+
9
+
*`void push(int x)` Pushes element x to the back of the queue.
10
+
*`int pop()` Removes the element from the front of the queue and returns it.
11
+
*`int peek()` Returns the element at the front of the queue.
12
+
*`boolean empty()` Returns `true` if the queue is empty, `false` otherwise.
13
+
14
+
**Notes:**
15
+
16
+
* You must use **only** standard operations of a stack, which means only `push to top`, `peek/pop from top`, `size`, and `is empty` operations are valid.
17
+
* Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
35
+
myQueue.peek(); // return 1
36
+
myQueue.pop(); // return 1, queue is [2]
37
+
myQueue.empty(); // return false
38
+
39
+
**Constraints:**
40
+
41
+
*`1 <= x <= 9`
42
+
* At most `100` calls will be made to `push`, `pop`, `peek`, and `empty`.
43
+
* All the calls to `pop` and `peek` are valid.
44
+
45
+
**Follow-up:** Can you implement the queue such that each operation is **[amortized](https://en.wikipedia.org/wiki/Amortized_analysis)**`O(1)` time complexity? In other words, performing `n` operations will take overall `O(n)` time even if one of those operations may take longer.
235\. Lowest Common Ancestor of a Binary Search Tree
2
+
3
+
Medium
4
+
5
+
Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.
6
+
7
+
According to the [definition of LCA on Wikipedia](https://en.wikipedia.org/wiki/Lowest_common_ancestor): “The lowest common ancestor is defined between two nodes `p` and `q` as the lowest node in `T` that has both `p` and `q` as descendants (where we allow **a node to be a descendant of itself**).”
0 commit comments