-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonotonicStack.js
More file actions
157 lines (139 loc) · 3.74 KB
/
monotonicStack.js
File metadata and controls
157 lines (139 loc) · 3.74 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
class MinStack {
constructor() {
this.stack = [];
this.minStack = [];
}
push(val) {
this.stack.push(val);
if (this.minStack.length === 0 || val <= this.minStack[this.minStack.length - 1]) {
this.minStack.push(val);
}
}
pop() {
if (this.stack.length === 0) return null;
let removed = this.stack.pop();
if (removed === this.minStack[this.minStack.length - 1]) {
this.minStack.pop();
}
return removed;
}
top() {
return this.stack.length ? this.stack[this.stack.length - 1] : null;
}
getMin() {
return this.minStack.length ? this.minStack[this.minStack.length - 1] : null;
}
}
class MaxStack {
constructor() {
this.stack = [];
this.maxStack = [];
}
push(val) {
this.stack.push(val);
if (this.maxStack.length === 0 || val >= this.maxStack[this.maxStack.length - 1]) {
this.maxStack.push(val);
}
}
pop() {
if (this.stack.length === 0) return null;
let removed = this.stack.pop();
if (removed === this.maxStack[this.maxStack.length - 1]) {
this.maxStack.pop();
}
return removed;
}
top() {
return this.stack.length ? this.stack[this.stack.length - 1] : null;
}
getMax() {
return this.maxStack.length ? this.maxStack[this.maxStack.length - 1] : null;
}
}
const minStack = new MinStack();
// minStack.push(3);
// minStack.push(5);
// minStack.push(2);
// console.log(minStack.getMin());
// minStack.pop();
// console.log(minStack.getMin());
// const maxStack = new MaxStack();
// maxStack.push(1);
// maxStack.push(4);
// maxStack.push(2);
// console.log(maxStack.getMax());
// maxStack.pop();
// console.log(maxStack.getMax());
class MinStack1 {
constructor(){
this.stack = []
this.minStack = []
}
push(val){
this.stack.push(val)
if(this.minStack.length == 0 || val <= this.minStack[this.minStack.length - 1]){
this.minStack.push(val)
}
}
pop(){
if(this.stack.length == 0) return null
let removed = this.stack.pop()
if(removed == this.minStack[this.minStack.length -1]){
this.minStack.pop()
}
return removed
}
top(){
return this.stack.length ? this.stack[this.stack.length - 1] : null
}
getMin(){
return this.minStack.length ? this.minStack[this.minStack.length - 1] : null
}
}
const minStack1 = new MinStack1()
minStack1.push(10)
// minStack1.push(20)
// minStack1.push(30)
// minStack1.push(40)
// console.log(minStack1.getMin())
// console.log(minStack1.top())
// minStack1.pop()
// console.log(minStack1.getMin())
class MaxStack1 {
constructor(){
this.stack = []
this.maxStack = []
}
push(val){
this.stack.push(val)
if(this.maxStack.length == 0 || val >= this.maxStack[this.maxStack.length - 1]){
this.maxStack.push(val)
}
}
pop(){
if(this.stack.length == 0) return null
let removed = this.stack.pop()
if(removed == this.maxStack[this.maxStack.length - 1]){
this.maxStack.pop()
}
return removed
}
top(){
return this.stack.length ? this.stack[this.stack.length - 1] : null
}
getMax(){
return this.maxStack.length ? this.maxStack[this.maxStack.length - 1] : null
}
}
const maxStack1 = new MaxStack1()
// maxStack1.push(30)
// maxStack1.push(40)
// maxStack1.push(20)
// maxStack1.push(10)
// maxStack1.push(50)
// maxStack1.push(5)
// console.log(maxStack1.top())
// console.log(maxStack1.getMax())
// maxStack1.pop()
// maxStack1.pop()
// console.log(maxStack1.getMax())