Skip to content

Commit 82af58b

Browse files
committed
leetcode
1 parent f519717 commit 82af58b

File tree

4 files changed

+270
-0
lines changed

4 files changed

+270
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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 }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
type MyQueue struct {
4+
stack1 Stack
5+
stack2 Stack
6+
}
7+
8+
func Constructor() MyQueue {
9+
return *new(MyQueue)
10+
}
11+
12+
func (this *MyQueue) Push(x int) {
13+
14+
for !this.stack1.IsEmpty() {
15+
pop, _ := this.stack1.Pop()
16+
this.stack2.Push(pop)
17+
}
18+
19+
this.stack1.Push(x)
20+
21+
for !this.stack2.IsEmpty() {
22+
pop, _ := this.stack2.Pop()
23+
this.stack1.Push(pop)
24+
}
25+
}
26+
27+
func (this *MyQueue) Pop() int {
28+
pop, _ := this.stack1.Pop()
29+
return pop
30+
}
31+
32+
func (this *MyQueue) Peek() int {
33+
peek, _ := this.stack1.Peek()
34+
return peek
35+
}
36+
37+
func (this *MyQueue) Empty() bool {
38+
return this.stack1.IsEmpty()
39+
}
40+
41+
type Stack []int
42+
43+
func (s *Stack) IsEmpty() bool {
44+
return len(*s) == 0
45+
}
46+
func (s *Stack) Push(num int) {
47+
*s = append(*s, num)
48+
}
49+
func (s *Stack) Len() int {
50+
return len(*s)
51+
}
52+
func (s *Stack) Pop() (int, bool) {
53+
if s.IsEmpty() {
54+
return 0, false
55+
} else {
56+
index := len(*s) - 1
57+
elem := (*s)[index]
58+
*s = (*s)[:index]
59+
return elem, true
60+
}
61+
}
62+
63+
func (s *Stack) Peek() (int, bool) {
64+
if s.IsEmpty() {
65+
return 0, false
66+
} else {
67+
index := len(*s) - 1
68+
elem := (*s)[index]
69+
return elem, true
70+
}
71+
}

ImplementQueueUsingStacks/implement_queue_using_stacks.md

Whitespace-only changes.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
8282
- [Invert Binary Tree](InvertBinaryTree/invert_binary_tree.dart)
8383
- [Summary Ranges](SummaryRanges/summary_ranges.dart)
8484
- [Power of Two](PowerOfTwo/power_of_two.dart)
85+
- [Implement Queue using Stacks](ImplementQueueUsingStacks/implement_queue_using_stacks.dart)
8586

8687
## Reach me via
8788

0 commit comments

Comments
 (0)