Skip to content

Commit ac18394

Browse files
committed
leetcode
1 parent e75935b commit ac18394

File tree

4 files changed

+320
-0
lines changed

4 files changed

+320
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
3+
-* Implement Stack using Queues *-
4+
5+
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).
6+
7+
Implement the MyStack class:
8+
9+
void push(int x) Pushes element x to the top of the stack.
10+
int pop() Removes the element on the top of the stack and returns it.
11+
int top() Returns the element on the top of the stack.
12+
boolean empty() Returns true if the stack is empty, false otherwise.
13+
Notes:
14+
15+
You must use only standard operations of a queue, which means that only push to back, peek/pop from front, size and is empty operations are valid.
16+
Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue) as long as you use only a queue's standard operations.
17+
18+
19+
Example 1:
20+
21+
Input
22+
["MyStack", "push", "push", "top", "pop", "empty"]
23+
[[], [1], [2], [], [], []]
24+
Output
25+
[null, null, null, 2, 2, false]
26+
27+
Explanation
28+
MyStack myStack = new MyStack();
29+
myStack.push(1);
30+
myStack.push(2);
31+
myStack.top(); // return 2
32+
myStack.pop(); // return 2
33+
myStack.empty(); // return False
34+
35+
36+
Constraints:
37+
38+
1 <= x <= 9
39+
At most 100 calls will be made to push, pop, top, and empty.
40+
All the calls to pop and top are valid.
41+
42+
43+
Follow-up: Can you implement the stack using only one queue?
44+
45+
46+
*/
47+
48+
import 'dart:collection';
49+
50+
// Runtime: 469 ms, faster than 50.00% of Dart online submissions for Implement Stack using Queues.
51+
// Memory Usage: 143 MB, less than 50.00% of Dart online submissions for Implement Stack using Queues.
52+
53+
class MyStack {
54+
Queue<int> queueOne = Queue();
55+
Queue<int> queueTwo = Queue();
56+
MyStack() {
57+
this.queueOne;
58+
this.queueTwo;
59+
}
60+
61+
void push(int x) {
62+
if (queueOne.isEmpty && queueTwo.isEmpty) {
63+
queueOne.add(x);
64+
} else if (queueOne.isEmpty) {
65+
queueOne.add(x);
66+
while (queueTwo.isNotEmpty) {
67+
queueOne.add(queueTwo.removeFirst());
68+
}
69+
} else if (queueTwo.isEmpty) {
70+
queueTwo.add(x);
71+
while (queueOne.isNotEmpty) {
72+
queueTwo.add(queueOne.removeFirst());
73+
}
74+
}
75+
}
76+
77+
int pop() {
78+
if (queueOne.isNotEmpty) {
79+
return queueOne.removeFirst();
80+
} else if (queueTwo.isNotEmpty)
81+
return queueTwo.removeFirst();
82+
else
83+
return -1;
84+
}
85+
86+
int top() {
87+
if (queueOne.isNotEmpty) {
88+
return queueOne.first;
89+
} else if (queueTwo.isNotEmpty)
90+
return queueTwo.first;
91+
else
92+
return -1;
93+
}
94+
95+
bool empty() {
96+
return queueTwo.isEmpty && queueOne.isEmpty;
97+
}
98+
}
99+
100+
/*
101+
102+
class MyStack {
103+
104+
MyStack() {
105+
106+
}
107+
108+
void push(int x) {
109+
110+
}
111+
112+
int pop() {
113+
114+
}
115+
116+
int top() {
117+
118+
}
119+
120+
bool empty() {
121+
122+
}
123+
}
124+
125+
*/
126+
class MyStacking {
127+
// Runtime: 467 ms, faster than 50.00% of Dart online submissions for Implement Stack using Queues.
128+
// Memory Usage: 143 MB, less than 75.00% of Dart online submissions for Implement Stack using Queues.
129+
Queue<int> queue = Queue();
130+
131+
MyStacking() {
132+
this.queue;
133+
}
134+
135+
void push(int x) {
136+
queue.add(x);
137+
for (int i = 0; i < queue.length - 1; i++) {
138+
queue.add(queue.removeFirst());
139+
}
140+
}
141+
142+
int pop() {
143+
return queue.removeFirst();
144+
}
145+
146+
int top() {
147+
return queue.first;
148+
}
149+
150+
bool empty() {
151+
return queue.isEmpty;
152+
}
153+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
// Runtime: 0 ms, faster than 100.00% of Go online submissions for Implement Stack using Queues.
4+
// Memory Usage: 1.9 MB, less than 82.31% of Go online submissions for Implement Stack using Queues.
5+
type MyStack struct {
6+
queue []int
7+
}
8+
9+
/** Initialize your data structure here. */
10+
func Constructor() MyStack {
11+
return MyStack{[]int{}}
12+
}
13+
14+
/** Push element x onto stack. */
15+
func (this *MyStack) Push(x int) {
16+
this.queue = append(this.queue[:0], append([]int{x}, this.queue[0:]...)...) // prepend
17+
}
18+
19+
/** Removes the element on top of the stack and returns that element. */
20+
func (this *MyStack) Pop() int {
21+
temp := this.queue[0]
22+
this.queue = this.queue[1:]
23+
return temp
24+
}
25+
26+
/** Get the top element. */
27+
func (this *MyStack) Top() int {
28+
return this.queue[0]
29+
}
30+
31+
/** Returns whether the stack is empty. */
32+
func (this *MyStack) Empty() bool {
33+
return len(this.queue) == 0
34+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# 🔥 Implement Stack using Queues 🔥 || 3 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1 Single Queue
4+
5+
```dart
6+
import 'dart:collection';
7+
8+
class MyStack {
9+
// Runtime: 467 ms, faster than 50.00% of Dart online submissions for Implement Stack using Queues.
10+
// Memory Usage: 143 MB, less than 75.00% of Dart online submissions for Implement Stack using Queues.
11+
Queue<int> queue = Queue();
12+
13+
MyStack() {
14+
this.queue;
15+
}
16+
17+
void push(int x) {
18+
queue.add(x);
19+
for (int i = 0; i < queue.length - 1; i++) {
20+
queue.add(queue.removeFirst());
21+
}
22+
}
23+
24+
int pop() {
25+
return queue.removeFirst();
26+
}
27+
28+
int top() {
29+
return queue.first;
30+
}
31+
32+
bool empty() {
33+
return queue.isEmpty;
34+
}
35+
}
36+
```
37+
38+
## Solution - 2 Double Queue
39+
40+
```dart
41+
import 'dart:collection';
42+
43+
class MyStack {
44+
// Runtime: 469 ms, faster than 50.00% of Dart online submissions for Implement Stack using Queues.
45+
// Memory Usage: 143 MB, less than 50.00% of Dart online submissions for Implement Stack using Queues.
46+
47+
Queue<int> queueOne = Queue();
48+
Queue<int> queueTwo = Queue();
49+
MyStack() {
50+
this.queueOne;
51+
this.queueTwo;
52+
}
53+
54+
void push(int x) {
55+
if (queueOne.isEmpty && queueTwo.isEmpty) {
56+
queueOne.add(x);
57+
} else if (queueOne.isEmpty) {
58+
queueOne.add(x);
59+
while (queueTwo.isNotEmpty) {
60+
queueOne.add(queueTwo.removeFirst());
61+
}
62+
} else if (queueTwo.isEmpty) {
63+
queueTwo.add(x);
64+
while (queueOne.isNotEmpty) {
65+
queueTwo.add(queueOne.removeFirst());
66+
}
67+
}
68+
}
69+
70+
int pop() {
71+
if (queueOne.isNotEmpty) {
72+
return queueOne.removeFirst();
73+
} else if (queueTwo.isNotEmpty)
74+
return queueTwo.removeFirst();
75+
else
76+
return -1;
77+
}
78+
79+
int top() {
80+
if (queueOne.isNotEmpty) {
81+
return queueOne.first;
82+
} else if (queueTwo.isNotEmpty)
83+
return queueTwo.first;
84+
else
85+
return -1;
86+
}
87+
88+
bool empty() {
89+
return queueTwo.isEmpty && queueOne.isEmpty;
90+
}
91+
}
92+
```
93+
94+
## Bonus Solution - Go 😈
95+
96+
```go
97+
type MyStack struct {
98+
queue []int
99+
}
100+
101+
102+
/** Initialize your data structure here. */
103+
func Constructor() MyStack {
104+
return MyStack{ []int{} }
105+
}
106+
107+
108+
/** Push element x onto stack. */
109+
func (this *MyStack) Push(x int) {
110+
this.queue = append(this.queue[:0], append([]int{x}, this.queue[0:]...)...) // prepend
111+
}
112+
113+
114+
/** Removes the element on top of the stack and returns that element. */
115+
func (this *MyStack) Pop() int {
116+
temp := this.queue[0]
117+
this.queue = this.queue[1:]
118+
return temp
119+
}
120+
121+
122+
/** Get the top element. */
123+
func (this *MyStack) Top() int {
124+
return this.queue[0]
125+
}
126+
127+
128+
/** Returns whether the stack is empty. */
129+
func (this *MyStack) Empty() bool {
130+
return len(this.queue) == 0
131+
}
132+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
7878
- [Reverse Linked List](ReverseLinkedList/reverse_linked_list.dart)
7979
- [Contains Duplicate](ContainsDuplicate/contains_duplicate.dart)
8080
- [Contains Duplicate II](ContainsDuplicate-II/contains_duplicate_II.dart)
81+
- [Implement Stack using Queues](ImplementStackUsingQueues/implement_stack_using_queues.dart)
8182

8283
## Reach me via
8384

0 commit comments

Comments
 (0)