-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminStack.js
More file actions
51 lines (44 loc) · 1.55 KB
/
minStack.js
File metadata and controls
51 lines (44 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class MinStack {
constructor() {
this.stack = [] // Use an array to represent the stack
this.corrospondingStackMinVal = []
}
push(val) {
const currentMinValStackIdx = this.corrospondingStackMinVal.length > 0 ? this.corrospondingStackMinVal.length - 1 : 0
this.stack.push(val)
if ((val <= this.getMin() || this.corrospondingStackMinVal.length === 0)) {
this.corrospondingStackMinVal.push(val)
}
}
pop() {
if (this.stack.length > 0) {
const popped = this.stack.pop()
const currentMinValStackIdx = this.corrospondingStackMinVal.length > 0 ? this.corrospondingStackMinVal.length - 1 : 0
if (popped === this.corrospondingStackMinVal[currentMinValStackIdx]) {
this.corrospondingStackMinVal.pop()
}
return popped
}
}
top() {
return this.stack[this.stack.length - 1]
}
getMin() {
const currentMinValStackIdx = this.corrospondingStackMinVal.length > 0 ? this.corrospondingStackMinVal.length - 1 : 0
return this.corrospondingStackMinVal[currentMinValStackIdx]
}
}
// Instantiate a MinStack object
const minStack = new MinStack()
// Perform operations based on the given input
minStack.push(-2)
minStack.push(0)
minStack.push(-3)
const output1 = minStack.getMin() // Return -3
minStack.pop()
const output2 = minStack.top() // Return 0
const output3 = minStack.getMin() // Return -2
// Compare the outputs with the expected output
console.log("Output 1:", output1) // Expected: -3
console.log("Output 2:", output2) // Expected: 0
console.log("Output 3:", output3) // Expected: -2