Skip to content

Commit

Permalink
added MinStack.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Naughton Jr authored and Kevin Naughton Jr committed May 29, 2018
1 parent c41d2e8 commit a58514b
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 0 deletions.
55 changes: 55 additions & 0 deletions company/amazon/MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//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.

/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
class MinStack {
class Node {
int data;
int min;
Node next;

public Node(int data, int min) {
this.data = data;
this.min = min;
this.next = null;
}
}
Node head;

/** initialize your data structure here. */
public MinStack() {

}

public void push(int x) {
if(head == null) {
head = new Node(x, x);
} else {
Node newNode = new Node(x, Math.min(x, head.min));
newNode.next = head;
head = newNode;
}
}

public void pop() {
head = head.next;
}

public int top() {
return head.data;
}

public int getMin() {
return head.min;
}
}
55 changes: 55 additions & 0 deletions company/bloomberg/MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//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.

/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
class MinStack {
class Node {
int data;
int min;
Node next;

public Node(int data, int min) {
this.data = data;
this.min = min;
this.next = null;
}
}
Node head;

/** initialize your data structure here. */
public MinStack() {

}

public void push(int x) {
if(head == null) {
head = new Node(x, x);
} else {
Node newNode = new Node(x, Math.min(x, head.min));
newNode.next = head;
head = newNode;
}
}

public void pop() {
head = head.next;
}

public int top() {
return head.data;
}

public int getMin() {
return head.min;
}
}
55 changes: 55 additions & 0 deletions company/google/MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//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.

/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
class MinStack {
class Node {
int data;
int min;
Node next;

public Node(int data, int min) {
this.data = data;
this.min = min;
this.next = null;
}
}
Node head;

/** initialize your data structure here. */
public MinStack() {

}

public void push(int x) {
if(head == null) {
head = new Node(x, x);
} else {
Node newNode = new Node(x, Math.min(x, head.min));
newNode.next = head;
head = newNode;
}
}

public void pop() {
head = head.next;
}

public int top() {
return head.data;
}

public int getMin() {
return head.min;
}
}
55 changes: 55 additions & 0 deletions company/snapchat/MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//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.

/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
class MinStack {
class Node {
int data;
int min;
Node next;

public Node(int data, int min) {
this.data = data;
this.min = min;
this.next = null;
}
}
Node head;

/** initialize your data structure here. */
public MinStack() {

}

public void push(int x) {
if(head == null) {
head = new Node(x, x);
} else {
Node newNode = new Node(x, Math.min(x, head.min));
newNode.next = head;
head = newNode;
}
}

public void pop() {
head = head.next;
}

public int top() {
return head.data;
}

public int getMin() {
return head.min;
}
}
55 changes: 55 additions & 0 deletions company/uber/MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//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.

/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
class MinStack {
class Node {
int data;
int min;
Node next;

public Node(int data, int min) {
this.data = data;
this.min = min;
this.next = null;
}
}
Node head;

/** initialize your data structure here. */
public MinStack() {

}

public void push(int x) {
if(head == null) {
head = new Node(x, x);
} else {
Node newNode = new Node(x, Math.min(x, head.min));
newNode.next = head;
head = newNode;
}
}

public void pop() {
head = head.next;
}

public int top() {
return head.data;
}

public int getMin() {
return head.min;
}
}
55 changes: 55 additions & 0 deletions leetcode/design/MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//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.

/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
class MinStack {
class Node {
int data;
int min;
Node next;

public Node(int data, int min) {
this.data = data;
this.min = min;
this.next = null;
}
}
Node head;

/** initialize your data structure here. */
public MinStack() {

}

public void push(int x) {
if(head == null) {
head = new Node(x, x);
} else {
Node newNode = new Node(x, Math.min(x, head.min));
newNode.next = head;
head = newNode;
}
}

public void pop() {
head = head.next;
}

public int top() {
return head.data;
}

public int getMin() {
return head.min;
}
}

0 comments on commit a58514b

Please sign in to comment.