Skip to content

Commit ddeb7eb

Browse files
committed
update: min stack class added
1 parent 75eada6 commit ddeb7eb

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

src/_DataStructures_/Stack/baseball-game/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ function sumOfPoints(arr) {
2626
throw new Error('Invalid Argument!');
2727
}
2828
// Track the value of `sum` accordingly
29-
for (let el of arr) {
29+
// eslint-disable-next-line no-restricted-syntax
30+
for (const el of arr) {
3031
// console.log('data : ', pointsTracker.data);
3132
// console.log('sum : ', sum);
3233

@@ -52,8 +53,8 @@ function sumOfPoints(arr) {
5253
pointsTracker.push(result);
5354
} else {
5455
// push to the Stack if the value is integer
55-
sum += parseInt(el);
56-
pointsTracker.push(parseInt(el));
56+
sum += parseInt(el, 10);
57+
pointsTracker.push(parseInt(el, 10));
5758
}
5859
}
5960
return sum;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
You have to implement the minStack class which will have a min() function.
3+
Whenever min() is called, the minimum value of the stack is returned in O(1) time.
4+
The element is not popped from the stack, its value is simply returned.
5+
6+
Keep in mind that the min function should work in O(1) and should not pop
7+
the minimum element out of the stack. It simply returns its value.
8+
*/
9+
10+
const Stack = require('../index');
11+
12+
class MinStack {
13+
constructor() {
14+
this.main = new Stack();
15+
this.minStack = new Stack();
16+
}
17+
18+
push(element) {
19+
this.main.push(element);
20+
21+
if (!this.minStack.peek()) {
22+
return this.minStack.push(element);
23+
}
24+
if (element > this.minStack.peek()) {
25+
return this.minStack.push(this.minStack.peek());
26+
}
27+
return this.minStack.push(element);
28+
}
29+
30+
pop() {
31+
this.minStack.pop();
32+
return this.main.pop();
33+
}
34+
35+
getMin() {
36+
return this.minStack.peek();
37+
}
38+
}
39+
40+
const ms = new MinStack();
41+
42+
ms.push(1);
43+
ms.push(10);
44+
ms.push(21);
45+
ms.push(3);
46+
ms.push(9);
47+
ms.push(-11);
48+
ms.push(32);
49+
50+
// eslint-disable-next-line no-console
51+
console.log(ms.minStack.data);
52+
// eslint-disable-next-line no-console
53+
console.log(ms.getMin());
54+
55+
ms.pop();
56+
ms.pop();
57+
58+
// eslint-disable-next-line no-console
59+
console.log(ms.getMin());

0 commit comments

Comments
 (0)