Skip to content

Commit a58514b

Browse files
Kevin Naughton JrKevin Naughton Jr
authored andcommitted
added MinStack.java
1 parent c41d2e8 commit a58514b

File tree

6 files changed

+330
-0
lines changed

6 files changed

+330
-0
lines changed

company/amazon/MinStack.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
2+
//push(x) -- Push element x onto stack.
3+
//pop() -- Removes the element on top of the stack.
4+
//top() -- Get the top element.
5+
//getMin() -- Retrieve the minimum element in the stack.
6+
7+
/**
8+
* Your MinStack object will be instantiated and called as such:
9+
* MinStack obj = new MinStack();
10+
* obj.push(x);
11+
* obj.pop();
12+
* int param_3 = obj.top();
13+
* int param_4 = obj.getMin();
14+
*/
15+
class MinStack {
16+
class Node {
17+
int data;
18+
int min;
19+
Node next;
20+
21+
public Node(int data, int min) {
22+
this.data = data;
23+
this.min = min;
24+
this.next = null;
25+
}
26+
}
27+
Node head;
28+
29+
/** initialize your data structure here. */
30+
public MinStack() {
31+
32+
}
33+
34+
public void push(int x) {
35+
if(head == null) {
36+
head = new Node(x, x);
37+
} else {
38+
Node newNode = new Node(x, Math.min(x, head.min));
39+
newNode.next = head;
40+
head = newNode;
41+
}
42+
}
43+
44+
public void pop() {
45+
head = head.next;
46+
}
47+
48+
public int top() {
49+
return head.data;
50+
}
51+
52+
public int getMin() {
53+
return head.min;
54+
}
55+
}

company/bloomberg/MinStack.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
2+
//push(x) -- Push element x onto stack.
3+
//pop() -- Removes the element on top of the stack.
4+
//top() -- Get the top element.
5+
//getMin() -- Retrieve the minimum element in the stack.
6+
7+
/**
8+
* Your MinStack object will be instantiated and called as such:
9+
* MinStack obj = new MinStack();
10+
* obj.push(x);
11+
* obj.pop();
12+
* int param_3 = obj.top();
13+
* int param_4 = obj.getMin();
14+
*/
15+
class MinStack {
16+
class Node {
17+
int data;
18+
int min;
19+
Node next;
20+
21+
public Node(int data, int min) {
22+
this.data = data;
23+
this.min = min;
24+
this.next = null;
25+
}
26+
}
27+
Node head;
28+
29+
/** initialize your data structure here. */
30+
public MinStack() {
31+
32+
}
33+
34+
public void push(int x) {
35+
if(head == null) {
36+
head = new Node(x, x);
37+
} else {
38+
Node newNode = new Node(x, Math.min(x, head.min));
39+
newNode.next = head;
40+
head = newNode;
41+
}
42+
}
43+
44+
public void pop() {
45+
head = head.next;
46+
}
47+
48+
public int top() {
49+
return head.data;
50+
}
51+
52+
public int getMin() {
53+
return head.min;
54+
}
55+
}

company/google/MinStack.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
2+
//push(x) -- Push element x onto stack.
3+
//pop() -- Removes the element on top of the stack.
4+
//top() -- Get the top element.
5+
//getMin() -- Retrieve the minimum element in the stack.
6+
7+
/**
8+
* Your MinStack object will be instantiated and called as such:
9+
* MinStack obj = new MinStack();
10+
* obj.push(x);
11+
* obj.pop();
12+
* int param_3 = obj.top();
13+
* int param_4 = obj.getMin();
14+
*/
15+
class MinStack {
16+
class Node {
17+
int data;
18+
int min;
19+
Node next;
20+
21+
public Node(int data, int min) {
22+
this.data = data;
23+
this.min = min;
24+
this.next = null;
25+
}
26+
}
27+
Node head;
28+
29+
/** initialize your data structure here. */
30+
public MinStack() {
31+
32+
}
33+
34+
public void push(int x) {
35+
if(head == null) {
36+
head = new Node(x, x);
37+
} else {
38+
Node newNode = new Node(x, Math.min(x, head.min));
39+
newNode.next = head;
40+
head = newNode;
41+
}
42+
}
43+
44+
public void pop() {
45+
head = head.next;
46+
}
47+
48+
public int top() {
49+
return head.data;
50+
}
51+
52+
public int getMin() {
53+
return head.min;
54+
}
55+
}

company/snapchat/MinStack.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
2+
//push(x) -- Push element x onto stack.
3+
//pop() -- Removes the element on top of the stack.
4+
//top() -- Get the top element.
5+
//getMin() -- Retrieve the minimum element in the stack.
6+
7+
/**
8+
* Your MinStack object will be instantiated and called as such:
9+
* MinStack obj = new MinStack();
10+
* obj.push(x);
11+
* obj.pop();
12+
* int param_3 = obj.top();
13+
* int param_4 = obj.getMin();
14+
*/
15+
class MinStack {
16+
class Node {
17+
int data;
18+
int min;
19+
Node next;
20+
21+
public Node(int data, int min) {
22+
this.data = data;
23+
this.min = min;
24+
this.next = null;
25+
}
26+
}
27+
Node head;
28+
29+
/** initialize your data structure here. */
30+
public MinStack() {
31+
32+
}
33+
34+
public void push(int x) {
35+
if(head == null) {
36+
head = new Node(x, x);
37+
} else {
38+
Node newNode = new Node(x, Math.min(x, head.min));
39+
newNode.next = head;
40+
head = newNode;
41+
}
42+
}
43+
44+
public void pop() {
45+
head = head.next;
46+
}
47+
48+
public int top() {
49+
return head.data;
50+
}
51+
52+
public int getMin() {
53+
return head.min;
54+
}
55+
}

company/uber/MinStack.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
2+
//push(x) -- Push element x onto stack.
3+
//pop() -- Removes the element on top of the stack.
4+
//top() -- Get the top element.
5+
//getMin() -- Retrieve the minimum element in the stack.
6+
7+
/**
8+
* Your MinStack object will be instantiated and called as such:
9+
* MinStack obj = new MinStack();
10+
* obj.push(x);
11+
* obj.pop();
12+
* int param_3 = obj.top();
13+
* int param_4 = obj.getMin();
14+
*/
15+
class MinStack {
16+
class Node {
17+
int data;
18+
int min;
19+
Node next;
20+
21+
public Node(int data, int min) {
22+
this.data = data;
23+
this.min = min;
24+
this.next = null;
25+
}
26+
}
27+
Node head;
28+
29+
/** initialize your data structure here. */
30+
public MinStack() {
31+
32+
}
33+
34+
public void push(int x) {
35+
if(head == null) {
36+
head = new Node(x, x);
37+
} else {
38+
Node newNode = new Node(x, Math.min(x, head.min));
39+
newNode.next = head;
40+
head = newNode;
41+
}
42+
}
43+
44+
public void pop() {
45+
head = head.next;
46+
}
47+
48+
public int top() {
49+
return head.data;
50+
}
51+
52+
public int getMin() {
53+
return head.min;
54+
}
55+
}

leetcode/design/MinStack.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
2+
//push(x) -- Push element x onto stack.
3+
//pop() -- Removes the element on top of the stack.
4+
//top() -- Get the top element.
5+
//getMin() -- Retrieve the minimum element in the stack.
6+
7+
/**
8+
* Your MinStack object will be instantiated and called as such:
9+
* MinStack obj = new MinStack();
10+
* obj.push(x);
11+
* obj.pop();
12+
* int param_3 = obj.top();
13+
* int param_4 = obj.getMin();
14+
*/
15+
class MinStack {
16+
class Node {
17+
int data;
18+
int min;
19+
Node next;
20+
21+
public Node(int data, int min) {
22+
this.data = data;
23+
this.min = min;
24+
this.next = null;
25+
}
26+
}
27+
Node head;
28+
29+
/** initialize your data structure here. */
30+
public MinStack() {
31+
32+
}
33+
34+
public void push(int x) {
35+
if(head == null) {
36+
head = new Node(x, x);
37+
} else {
38+
Node newNode = new Node(x, Math.min(x, head.min));
39+
newNode.next = head;
40+
head = newNode;
41+
}
42+
}
43+
44+
public void pop() {
45+
head = head.next;
46+
}
47+
48+
public int top() {
49+
return head.data;
50+
}
51+
52+
public int getMin() {
53+
return head.min;
54+
}
55+
}

0 commit comments

Comments
 (0)