forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kevin Naughton Jr
authored and
Kevin Naughton Jr
committed
May 29, 2018
1 parent
c41d2e8
commit a58514b
Showing
6 changed files
with
330 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |