Skip to content

Commit 65994b5

Browse files
committed
leetcode
1 parent c9a10c6 commit 65994b5

File tree

5 files changed

+488
-134
lines changed

5 files changed

+488
-134
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,198 +1,198 @@
1-
/*
1+
// /*
22

3-
-* Implement Queue using Stacks *-
3+
// -* Implement Queue using Stacks *-
44

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).
66

7-
Implement the MyQueue class:
7+
// Implement the MyQueue class:
88

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:
1414

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.
1717

1818

19-
Example 1:
19+
// Example 1:
2020

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]
2626

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
3434

3535

36-
Constraints:
36+
// Constraints:
3737

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.
4141

4242

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+
// /*
4480

45-
*/
4681

4782
// 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.
5083

51-
// Queue<int> ins = Queue();
52-
// Queue<int> out = Queue();
5384
// MyQueue() {
54-
// this.ins;
55-
// this.out;
85+
5686
// }
5787

5888
// void push(int x) {
59-
// ins.add(x);
89+
6090
// }
6191

6292
// int pop() {
63-
// if (out.isEmpty) while (ins.isNotEmpty) out.add(ins.removeLast());
6493

65-
// return out.removeLast();
6694
// }
6795

6896
// int peek() {
69-
// if (out.isEmpty) while (ins.isNotEmpty) out.add(ins.removeLast());
70-
// // peek
71-
// return out.first;
97+
7298
// }
7399

74100
// bool empty() {
75-
// return ins.isEmpty && out.isEmpty;
101+
76102
// }
77103
// }
78104

79-
/*
80-
81-
82-
class MyQueue {
83-
84-
MyQueue() {
85-
86-
}
87-
88-
void push(int x) {
89105

90-
}
106+
// */
91107

92-
int pop() {
108+
// abstract class Stack<T> {
109+
// // Pushes element to the top of the stack.
110+
// void push(T value);
93111

94-
}
112+
// // Removes the element at the top of the stack and returns it.
113+
// T pop();
95114

96-
int peek() {
115+
// // Returns the element at the top of the stack.
116+
// peek();
97117

98-
}
118+
// // Returns true if the stack is empty, false otherwise.
119+
// bool get isEmpty;
120+
// }
99121

100-
bool empty() {
122+
// abstract class Queue<T> {
123+
// // Pushes element [value] to the back of the queue.
124+
// void push(T value);
101125

102-
}
103-
}
126+
// // Removes the element from the front of the queue and returns it.
127+
// T pop();
104128

129+
// // Returns the element at the front of the queue.
130+
// T peek();
105131

106-
*/
132+
// // Returns true if the queue is empty, false otherwise.
133+
// bool get isEmpty;
134+
// }
107135

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);
111138

112-
// Removes the element at the top of the stack and returns it.
113-
T pop();
139+
// // final c.Queue<T> _internal;
114140

115-
// Returns the element at the top of the stack.
116-
peek();
141+
// // @override
142+
// // void push(T value) => _internal.addLast(value);
117143

118-
// Returns true if the stack is empty, false otherwise.
119-
bool get isEmpty;
120-
}
144+
// // @override
145+
// // T pop() => _internal.removeLast();
121146

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;
125149

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+
// // }
128153

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;
131159

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;
135162

136-
// class CollectionStack<T> implements Stack<T> {
137-
// CollectionStack(this._internal);
163+
// _Phase _phase;
138164

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+
// }
140175

141176
// @override
142-
// void push(T value) => _internal.addLast(value);
177+
// void push(T value) {
178+
// _switchPhase(_Phase.push);
179+
// _pushStack.push(value);
180+
// }
143181

144182
// @override
145-
// T pop() => _internal.removeLast();
183+
// T pop() {
184+
// _switchPhase(_Phase.pop);
185+
// return _popStack.pop();
186+
// }
146187

147188
// @override
148-
// T peek() => _internal.last;
189+
// T peek() {
190+
// _switchPhase(_Phase.pop);
191+
// return _popStack.peek();
192+
// }
149193

150194
// @override
151-
// bool get isEmpty => _internal.isEmpty;
195+
// bool get isEmpty => _pushStack.isEmpty && _popStack.isEmpty;
152196
// }
153197

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

Comments
 (0)