|
1 |
| -/* |
| 1 | +// /* |
2 | 2 |
|
3 |
| - -* Implement Queue using Stacks *- |
| 3 | +// -* Implement Queue using Stacks *- |
4 | 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). |
| 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 | 6 |
|
7 |
| -Implement the MyQueue class: |
| 7 | +// Implement the MyQueue class: |
8 | 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: |
| 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 | 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. |
| 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 | 17 |
|
18 | 18 |
|
19 |
| -Example 1: |
| 19 | +// Example 1: |
20 | 20 |
|
21 |
| -Input |
22 |
| -["MyQueue", "push", "push", "peek", "pop", "empty"] |
23 |
| -[[], [1], [2], [], [], []] |
24 |
| -Output |
25 |
| -[null, null, null, 1, 1, false] |
| 21 | +// Input |
| 22 | +// ["MyQueue", "push", "push", "peek", "pop", "empty"] |
| 23 | +// [[], [1], [2], [], [], []] |
| 24 | +// Output |
| 25 | +// [null, null, null, 1, 1, false] |
26 | 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 |
| 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 | 34 |
|
35 | 35 |
|
36 |
| -Constraints: |
| 36 | +// Constraints: |
37 | 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. |
| 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 | 41 |
|
42 | 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. |
| 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 | +// /* |
44 | 80 |
|
45 |
| -*/ |
46 | 81 |
|
47 | 82 | // 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 | 83 |
|
51 |
| -// Queue<int> ins = Queue(); |
52 |
| -// Queue<int> out = Queue(); |
53 | 84 | // MyQueue() {
|
54 |
| -// this.ins; |
55 |
| -// this.out; |
| 85 | + |
56 | 86 | // }
|
57 | 87 |
|
58 | 88 | // void push(int x) {
|
59 |
| -// ins.add(x); |
| 89 | + |
60 | 90 | // }
|
61 | 91 |
|
62 | 92 | // int pop() {
|
63 |
| -// if (out.isEmpty) while (ins.isNotEmpty) out.add(ins.removeLast()); |
64 | 93 |
|
65 |
| -// return out.removeLast(); |
66 | 94 | // }
|
67 | 95 |
|
68 | 96 | // int peek() {
|
69 |
| -// if (out.isEmpty) while (ins.isNotEmpty) out.add(ins.removeLast()); |
70 |
| -// // peek |
71 |
| -// return out.first; |
| 97 | + |
72 | 98 | // }
|
73 | 99 |
|
74 | 100 | // bool empty() {
|
75 |
| -// return ins.isEmpty && out.isEmpty; |
| 101 | + |
76 | 102 | // }
|
77 | 103 | // }
|
78 | 104 |
|
79 |
| -/* |
80 |
| -
|
81 |
| -
|
82 |
| -class MyQueue { |
83 |
| -
|
84 |
| - MyQueue() { |
85 |
| -
|
86 |
| - } |
87 |
| -
|
88 |
| - void push(int x) { |
89 | 105 |
|
90 |
| - } |
| 106 | +// */ |
91 | 107 |
|
92 |
| - int pop() { |
| 108 | +// abstract class Stack<T> { |
| 109 | +// // Pushes element to the top of the stack. |
| 110 | +// void push(T value); |
93 | 111 |
|
94 |
| - } |
| 112 | +// // Removes the element at the top of the stack and returns it. |
| 113 | +// T pop(); |
95 | 114 |
|
96 |
| - int peek() { |
| 115 | +// // Returns the element at the top of the stack. |
| 116 | +// peek(); |
97 | 117 |
|
98 |
| - } |
| 118 | +// // Returns true if the stack is empty, false otherwise. |
| 119 | +// bool get isEmpty; |
| 120 | +// } |
99 | 121 |
|
100 |
| - bool empty() { |
| 122 | +// abstract class Queue<T> { |
| 123 | +// // Pushes element [value] to the back of the queue. |
| 124 | +// void push(T value); |
101 | 125 |
|
102 |
| - } |
103 |
| -} |
| 126 | +// // Removes the element from the front of the queue and returns it. |
| 127 | +// T pop(); |
104 | 128 |
|
| 129 | +// // Returns the element at the front of the queue. |
| 130 | +// T peek(); |
105 | 131 |
|
106 |
| -*/ |
| 132 | +// // Returns true if the queue is empty, false otherwise. |
| 133 | +// bool get isEmpty; |
| 134 | +// } |
107 | 135 |
|
108 |
| -abstract class Stack<T> { |
109 |
| - // Pushes element to the top of the stack. |
110 |
| - void push(T value); |
| 136 | +// // class CollectionStack<T> implements Stack<T> { |
| 137 | +// // CollectionStack(this._internal); |
111 | 138 |
|
112 |
| - // Removes the element at the top of the stack and returns it. |
113 |
| - T pop(); |
| 139 | +// // final c.Queue<T> _internal; |
114 | 140 |
|
115 |
| - // Returns the element at the top of the stack. |
116 |
| - peek(); |
| 141 | +// // @override |
| 142 | +// // void push(T value) => _internal.addLast(value); |
117 | 143 |
|
118 |
| - // Returns true if the stack is empty, false otherwise. |
119 |
| - bool get isEmpty; |
120 |
| -} |
| 144 | +// // @override |
| 145 | +// // T pop() => _internal.removeLast(); |
121 | 146 |
|
122 |
| -abstract class Queue<T> { |
123 |
| - // Pushes element [value] to the back of the queue. |
124 |
| - void push(T value); |
| 147 | +// // @override |
| 148 | +// // T peek() => _internal.last; |
125 | 149 |
|
126 |
| - // Removes the element from the front of the queue and returns it. |
127 |
| - T pop(); |
| 150 | +// // @override |
| 151 | +// // bool get isEmpty => _internal.isEmpty; |
| 152 | +// // } |
128 | 153 |
|
129 |
| - // Returns the element at the front of the queue. |
130 |
| - T peek(); |
| 154 | +// class DoubleStackQueue<T> implements Queue<T> { |
| 155 | +// DoubleStackQueue( |
| 156 | +// this._pushStack, |
| 157 | +// this._popStack, |
| 158 | +// ) : _phase = _Phase.push; |
131 | 159 |
|
132 |
| - // Returns true if the queue is empty, false otherwise. |
133 |
| - bool get isEmpty; |
134 |
| -} |
| 160 | +// final Stack<T> _pushStack; |
| 161 | +// final Stack<T> _popStack; |
135 | 162 |
|
136 |
| -// class CollectionStack<T> implements Stack<T> { |
137 |
| -// CollectionStack(this._internal); |
| 163 | +// _Phase _phase; |
138 | 164 |
|
139 |
| -// final c.Queue<T> _internal; |
| 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 | +// } |
140 | 175 |
|
141 | 176 | // @override
|
142 |
| -// void push(T value) => _internal.addLast(value); |
| 177 | +// void push(T value) { |
| 178 | +// _switchPhase(_Phase.push); |
| 179 | +// _pushStack.push(value); |
| 180 | +// } |
143 | 181 |
|
144 | 182 | // @override
|
145 |
| -// T pop() => _internal.removeLast(); |
| 183 | +// T pop() { |
| 184 | +// _switchPhase(_Phase.pop); |
| 185 | +// return _popStack.pop(); |
| 186 | +// } |
146 | 187 |
|
147 | 188 | // @override
|
148 |
| -// T peek() => _internal.last; |
| 189 | +// T peek() { |
| 190 | +// _switchPhase(_Phase.pop); |
| 191 | +// return _popStack.peek(); |
| 192 | +// } |
149 | 193 |
|
150 | 194 | // @override
|
151 |
| -// bool get isEmpty => _internal.isEmpty; |
| 195 | +// bool get isEmpty => _pushStack.isEmpty && _popStack.isEmpty; |
152 | 196 | // }
|
153 | 197 |
|
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 } |
| 198 | +// enum _Phase { push, pop } |
0 commit comments