Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 298cd9b

Browse files
edit 155
1 parent 1bba8c6 commit 298cd9b

File tree

1 file changed

+57
-49
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+57
-49
lines changed

src/main/java/com/fishercoder/solutions/_155.java

Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,67 @@
22

33
import java.util.*;
44

5-
/**Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
6-
7-
push(x) -- Push element x onto stack.
8-
pop() -- Removes the element on top of the stack.
9-
top() -- Get the top element.
10-
getMin() -- Retrieve the minimum element in the stack.
11-
Example:
12-
_155 minStack = new _155();
13-
minStack.push(-2);
14-
minStack.push(0);
15-
minStack.push(-3);
16-
minStack.getMin(); --> Returns -3.
17-
minStack.pop();
18-
minStack.top(); --> Returns 0.
19-
minStack.getMin(); --> Returns -2.
5+
/**
6+
* 155. Min Stack
7+
* Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
8+
9+
* push(x) -- Push element x onto stack.
10+
* pop() -- Removes the element on top of the stack.
11+
* top() -- Get the top element.
12+
* getMin() -- Retrieve the minimum element in the stack.
13+
14+
* Example:
15+
* MinStack minStack = new MinStack();
16+
* minStack.push(-2);
17+
* minStack.push(0);
18+
* minStack.push(-3);
19+
* minStack.getMin(); --> Returns -3.
20+
* minStack.pop();
21+
* minStack.top(); --> Returns 0.
22+
* minStack.getMin(); --> Returns -2.
2023
*/
24+
2125
public class _155 {
22-
23-
private Stack<Integer> stack;
24-
private int min;
25-
26-
/** initialize your data structure here. */
27-
public _155() {
28-
stack = new Stack();
29-
min = Integer.MAX_VALUE;
30-
}
31-
32-
public void push(int x) {
33-
/**All the trick happens here, we push the second minimum number onto the stack before we push the newer one,
34-
* this way, when popping, we could always get the next minimum one in constant time.*/
35-
if(x <= min){
36-
stack.push(min);
37-
min = x;
26+
27+
public static class MinStack {
28+
private Stack<Integer> stack;
29+
private int min;
30+
31+
/**
32+
* initialize your data structure here.
33+
*/
34+
public MinStack() {
35+
stack = new Stack();
36+
min = Integer.MAX_VALUE;
3837
}
39-
stack.push(x);
40-
}
41-
42-
public void pop() {
43-
if(min == stack.peek()){
44-
stack.pop();
45-
min = stack.pop();
46-
} else {
47-
stack.pop();
38+
39+
public void push(int x) {
40+
/**All the trick happens here, we push the second minimum number onto the stack before we push the newer one,
41+
* this way, when popping, we could always get the next minimum one in constant time.*/
42+
if (x <= min) {
43+
stack.push(min);
44+
min = x;
45+
}
46+
stack.push(x);
47+
}
48+
49+
public void pop() {
50+
if (min == stack.peek()) {
51+
stack.pop();
52+
min = stack.pop();
53+
} else {
54+
stack.pop();
55+
}
56+
if (stack.isEmpty()) min = Integer.MAX_VALUE;
57+
}
58+
59+
public int top() {
60+
return stack.peek();
61+
}
62+
63+
public int getMin() {
64+
return min;
4865
}
49-
if(stack.isEmpty()) min = Integer.MAX_VALUE;
50-
}
51-
52-
public int top() {
53-
return stack.peek();
54-
}
55-
56-
public int getMin() {
57-
return min;
5866
}
5967

6068
}

0 commit comments

Comments
 (0)