|
| 1 | +/* |
| 2 | +
|
| 3 | + -* Implement Queue using Stacks *- |
| 4 | +
|
| 5 | + 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 | +Notes: |
| 14 | +
|
| 15 | +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. |
| 16 | +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. |
| 17 | +
|
| 18 | +
|
| 19 | +Example 1: |
| 20 | +
|
| 21 | +Input |
| 22 | +["MyQueue", "push", "push", "peek", "pop", "empty"] |
| 23 | +[[], [1], [2], [], [], []] |
| 24 | +Output |
| 25 | +[null, null, null, 1, 1, false] |
| 26 | +
|
| 27 | +Explanation |
| 28 | +MyQueue myQueue = new MyQueue(); |
| 29 | +myQueue.push(1); // queue is: [1] |
| 30 | +myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue) |
| 31 | +myQueue.peek(); // return 1 |
| 32 | +myQueue.pop(); // return 1, queue is [2] |
| 33 | +myQueue.empty(); // return false |
| 34 | +
|
| 35 | +
|
| 36 | +Constraints: |
| 37 | +
|
| 38 | +1 <= x <= 9 |
| 39 | +At most 100 calls will be made to push, pop, peek, and empty. |
| 40 | +All the calls to pop and peek are valid. |
| 41 | +
|
| 42 | +
|
| 43 | +Follow-up: Can you implement the queue such that each operation is amortized 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. |
| 44 | +
|
| 45 | +*/ |
| 46 | + |
| 47 | +// class MyQueue { |
| 48 | +// // Runtime: 571 ms, faster than 7.69% of Dart online submissions for Implement Queue using Stacks. |
| 49 | +// // Memory Usage: 145 MB, less than 15.38% of Dart online submissions for Implement Queue using Stacks. |
| 50 | + |
| 51 | +// Queue<int> ins = Queue(); |
| 52 | +// Queue<int> out = Queue(); |
| 53 | +// MyQueue() { |
| 54 | +// this.ins; |
| 55 | +// this.out; |
| 56 | +// } |
| 57 | + |
| 58 | +// void push(int x) { |
| 59 | +// ins.add(x); |
| 60 | +// } |
| 61 | + |
| 62 | +// int pop() { |
| 63 | +// if (out.isEmpty) while (ins.isNotEmpty) out.add(ins.removeLast()); |
| 64 | + |
| 65 | +// return out.removeLast(); |
| 66 | +// } |
| 67 | + |
| 68 | +// int peek() { |
| 69 | +// if (out.isEmpty) while (ins.isNotEmpty) out.add(ins.removeLast()); |
| 70 | +// // peek |
| 71 | +// return out.first; |
| 72 | +// } |
| 73 | + |
| 74 | +// bool empty() { |
| 75 | +// return ins.isEmpty && out.isEmpty; |
| 76 | +// } |
| 77 | +// } |
| 78 | + |
| 79 | +/* |
| 80 | +
|
| 81 | +
|
| 82 | +class MyQueue { |
| 83 | +
|
| 84 | + MyQueue() { |
| 85 | +
|
| 86 | + } |
| 87 | +
|
| 88 | + void push(int x) { |
| 89 | +
|
| 90 | + } |
| 91 | +
|
| 92 | + int pop() { |
| 93 | +
|
| 94 | + } |
| 95 | +
|
| 96 | + int peek() { |
| 97 | +
|
| 98 | + } |
| 99 | +
|
| 100 | + bool empty() { |
| 101 | +
|
| 102 | + } |
| 103 | +} |
| 104 | +
|
| 105 | +
|
| 106 | +*/ |
| 107 | + |
| 108 | +abstract class Stack<T> { |
| 109 | + // Pushes element to the top of the stack. |
| 110 | + void push(T value); |
| 111 | + |
| 112 | + // Removes the element at the top of the stack and returns it. |
| 113 | + T pop(); |
| 114 | + |
| 115 | + // Returns the element at the top of the stack. |
| 116 | + peek(); |
| 117 | + |
| 118 | + // Returns true if the stack is empty, false otherwise. |
| 119 | + bool get isEmpty; |
| 120 | +} |
| 121 | + |
| 122 | +abstract class Queue<T> { |
| 123 | + // Pushes element [value] to the back of the queue. |
| 124 | + void push(T value); |
| 125 | + |
| 126 | + // Removes the element from the front of the queue and returns it. |
| 127 | + T pop(); |
| 128 | + |
| 129 | + // Returns the element at the front of the queue. |
| 130 | + T peek(); |
| 131 | + |
| 132 | + // Returns true if the queue is empty, false otherwise. |
| 133 | + bool get isEmpty; |
| 134 | +} |
| 135 | + |
| 136 | +// class CollectionStack<T> implements Stack<T> { |
| 137 | +// CollectionStack(this._internal); |
| 138 | + |
| 139 | +// final c.Queue<T> _internal; |
| 140 | + |
| 141 | +// @override |
| 142 | +// void push(T value) => _internal.addLast(value); |
| 143 | + |
| 144 | +// @override |
| 145 | +// T pop() => _internal.removeLast(); |
| 146 | + |
| 147 | +// @override |
| 148 | +// T peek() => _internal.last; |
| 149 | + |
| 150 | +// @override |
| 151 | +// bool get isEmpty => _internal.isEmpty; |
| 152 | +// } |
| 153 | + |
| 154 | +class DoubleStackQueue<T> implements Queue<T> { |
| 155 | + DoubleStackQueue( |
| 156 | + this._pushStack, |
| 157 | + this._popStack, |
| 158 | + ) : _phase = _Phase.push; |
| 159 | + |
| 160 | + final Stack<T> _pushStack; |
| 161 | + final Stack<T> _popStack; |
| 162 | + |
| 163 | + _Phase _phase; |
| 164 | + |
| 165 | + // [phase] is the new phase. |
| 166 | + void _switchPhase(_Phase phase) { |
| 167 | + if (_phase == phase) return; |
| 168 | + if (phase == _Phase.push) { |
| 169 | + while (!_popStack.isEmpty) _pushStack.push(_popStack.pop()); |
| 170 | + } else { |
| 171 | + while (!_pushStack.isEmpty) _popStack.push(_pushStack.pop()); |
| 172 | + } |
| 173 | + _phase = phase; |
| 174 | + } |
| 175 | + |
| 176 | + @override |
| 177 | + void push(T value) { |
| 178 | + _switchPhase(_Phase.push); |
| 179 | + _pushStack.push(value); |
| 180 | + } |
| 181 | + |
| 182 | + @override |
| 183 | + T pop() { |
| 184 | + _switchPhase(_Phase.pop); |
| 185 | + return _popStack.pop(); |
| 186 | + } |
| 187 | + |
| 188 | + @override |
| 189 | + T peek() { |
| 190 | + _switchPhase(_Phase.pop); |
| 191 | + return _popStack.peek(); |
| 192 | + } |
| 193 | + |
| 194 | + @override |
| 195 | + bool get isEmpty => _pushStack.isEmpty && _popStack.isEmpty; |
| 196 | +} |
| 197 | + |
| 198 | +enum _Phase { push, pop } |
0 commit comments