File tree 2 files changed +63
-3
lines changed
src/_DataStructures_/Stack
2 files changed +63
-3
lines changed Original file line number Diff line number Diff line change @@ -26,7 +26,8 @@ function sumOfPoints(arr) {
26
26
throw new Error ( 'Invalid Argument!' ) ;
27
27
}
28
28
// 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 ) {
30
31
// console.log('data : ', pointsTracker.data);
31
32
// console.log('sum : ', sum);
32
33
@@ -52,8 +53,8 @@ function sumOfPoints(arr) {
52
53
pointsTracker . push ( result ) ;
53
54
} else {
54
55
// 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 ) ) ;
57
58
}
58
59
}
59
60
return sum ;
Original file line number Diff line number Diff line change
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 ( ) ) ;
You can’t perform that action at this time.
0 commit comments