Skip to content

Commit 070f1a6

Browse files
committed
Leet code day 4 challehge solved
1 parent cc30247 commit 070f1a6

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
var MyStack = /** @class */ (function () {
2+
function MyStack() {
3+
// create two queues, q1 and q2
4+
this.q1 = new Queue();
5+
this.q2 = new Queue();
6+
}
7+
MyStack.prototype.push = function (x) {
8+
this.q2.enqueue(x);
9+
// Dequeue all q1 and enqueue it to q2
10+
while (!this.q1.isEmpty()) {
11+
var poppedElement = this.q1.dequeue();
12+
if (poppedElement !== null) {
13+
this.q2.enqueue(poppedElement);
14+
}
15+
}
16+
// Enqueue all q2 to q1
17+
while (!this.q2.isEmpty()) {
18+
var poppedElement = this.q2.dequeue();
19+
if (poppedElement !== null) {
20+
this.q1.enqueue(poppedElement);
21+
}
22+
}
23+
// Clear q2
24+
this.q2.clear();
25+
};
26+
MyStack.prototype.pop = function () {
27+
return this.q1.dequeue();
28+
};
29+
MyStack.prototype.top = function () {
30+
return this.q1.front();
31+
};
32+
MyStack.prototype.empty = function () {
33+
return this.q1.isEmpty();
34+
};
35+
return MyStack;
36+
}());
37+
var Queue = /** @class */ (function () {
38+
function Queue() {
39+
this.items = [];
40+
}
41+
Queue.prototype.enqueue = function (x) {
42+
this.items.push(x);
43+
};
44+
Queue.prototype.dequeue = function () {
45+
var _a, _b;
46+
return (_b = (_a = this.items) === null || _a === void 0 ? void 0 : _a.shift()) !== null && _b !== void 0 ? _b : null;
47+
};
48+
Queue.prototype.front = function () {
49+
return this.items[0];
50+
};
51+
Queue.prototype.isEmpty = function () {
52+
return this.items.length === 0;
53+
};
54+
Queue.prototype.clear = function () {
55+
this.items = [];
56+
};
57+
return Queue;
58+
}());
59+
var obj = new MyStack();
60+
obj.push(10);
61+
obj.push(20);
62+
obj.push(30);
63+
obj.push(40);
64+
var param_2 = obj.pop();
65+
var param_2 = obj.pop();
66+
var param_2 = obj.pop();
67+
var param_3 = obj.top();
68+
var param_4 = obj.empty();
69+
console.log(param_4, param_3);
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
class MyStack {
2+
private q1: Queue<number>;
3+
private q2: Queue<number>;
4+
constructor() {
5+
// create two queues, q1 and q2
6+
this.q1 = new Queue<number>();
7+
this.q2 = new Queue<number>();
8+
}
9+
push(x: number): void {
10+
11+
this.q2.enqueue(x);
12+
13+
// Dequeue all q1 and enqueue it to q2
14+
while (!this.q1.isEmpty()) {
15+
let poppedElement: number | null = this.q1.dequeue()
16+
if (poppedElement !== null) {
17+
this.q2.enqueue(poppedElement);
18+
}
19+
}
20+
21+
// Enqueue all q2 to q1
22+
while (!this.q2.isEmpty()) {
23+
let poppedElement: number | null = this.q2.dequeue()
24+
if (poppedElement !== null) {
25+
this.q1.enqueue(poppedElement);
26+
}
27+
28+
}
29+
30+
// Clear q2
31+
this.q2.clear();
32+
}
33+
34+
pop(): number | null {
35+
return this.q1.dequeue();
36+
}
37+
38+
top(): number {
39+
return this.q1.front();
40+
}
41+
42+
empty(): boolean {
43+
return this.q1.isEmpty();
44+
}
45+
46+
47+
}
48+
49+
class Queue<T> {
50+
private items: T[];
51+
constructor() {
52+
this.items = [];
53+
}
54+
55+
enqueue(x: T): void {
56+
this.items.push(x);
57+
}
58+
59+
dequeue(): T | null {
60+
return this.items?.shift() ?? null;
61+
}
62+
63+
front(): T {
64+
return this.items[0];
65+
}
66+
67+
isEmpty(): boolean {
68+
return this.items.length === 0;
69+
}
70+
71+
clear(): void {
72+
this.items = [];
73+
}
74+
75+
}
76+
77+
78+
var obj = new MyStack()
79+
obj.push(10);
80+
obj.push(20);
81+
obj.push(30);
82+
obj.push(40);
83+
var param_2 = obj.pop()
84+
var param_2 = obj.pop()
85+
var param_2 = obj.pop()
86+
var param_3 = obj.top()
87+
var param_4 = obj.empty()
88+
89+
console.log(param_4, param_3);
90+

0 commit comments

Comments
 (0)