-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathNo155.min-stack.js
More file actions
146 lines (132 loc) · 2.88 KB
/
Copy pathNo155.min-stack.js
File metadata and controls
146 lines (132 loc) · 2.88 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
/**
* Difficulty:
* Easy
*
* Desc:
* Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
* push(x) -- Push element x onto stack.
* pop() -- Removes the element on top of the stack.
* top() -- Get the top element.
* getMin() -- Retrieve the minimum element in the stack.
*
* Example:
* MinStack minStack = new MinStack();
* minStack.push(-2);
* minStack.push(0);
* minStack.push(-3);
* minStack.getMin(); --> Returns -3.
* minStack.pop();
* minStack.top(); --> Returns 0.
* minStack.getMin(); --> Returns -2.
*/
/**
* initialize your data structure here.
*/
var MinStack = function() {
this.min = null;
this.datas = [];
};
/**
* @param {number} x
* @return {void}
*/
MinStack.prototype.push = function(x) {
this.datas.push(x);
if (this.min === null || x < this.min) this.min = x;
};
/**
* @return {void}
*/
MinStack.prototype.pop = function() {
const num = this.datas.pop();
if (num === this.min) {
this.min = null;
this.min = this.getMin();
}
return num;
};
/**
* @return {number}
*/
MinStack.prototype.top = function() {
return this.datas[this.datas.length - 1];
};
/**
* @return {number}
*/
MinStack.prototype.getMin = function() {
if (this.min !== null) return this.min;
for (let i = 0; i < this.datas.length; i += 1) {
const num = this.datas[i];
if (this.min === null || num < this.min) this.min = num;
}
return this.min;
};
/**
* Your MinStack object will be instantiated and called as such:
* var obj = Object.create(MinStack).createNew()
* obj.push(x)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.getMin()
*/
// ============================== Solution 2 ==============================
/**
* initialize your data structure here.
*/
var MinStack_2 = function() {
this.queue = []
this.sorted = []
};
MinStack_2.prototype.search = function(num) {
let i = 0
let j = this.sorted.length - 1
while (i <= j) {
const mid = Math.floor((i + j) / 2)
if (this.sorted[mid] === num) return mid
if (this.sorted[mid] < num) {
i = mid + 1
} else {
j = mid - 1
}
}
return i
}
/**
* @param {number} x
* @return {void}
*/
MinStack_2.prototype.push = function(x) {
const index = this.search(x)
this.sorted.splice(index, 0, x)
this.queue.push([index, x])
};
/**
* @return {void}
*/
MinStack_2.prototype.pop = function() {
const [index, num] = this.queue.pop()
this.sorted.splice(index, 1)
};
/**
* @return {number}
*/
MinStack_2.prototype.top = function() {
if (!this.queue.length) return null
return this.queue[this.queue.length - 1][1]
};
/**
* @return {number}
*/
MinStack_2.prototype.getMin = function() {
if (!this.sorted.length) return null
return this.sorted[0]
};
/**
* Your MinStack object will be instantiated and called as such:
* var obj = new MinStack()
* obj.push(x)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.getMin()
*/