Min Stack
class MinStack {
private Stack<Integer> stack = new Stack<Integer>();
private Stack<Integer> minStack = new Stack<Integer>();
public void push(int x) {
if (minStack.isEmpty() || x <= minStack.peek())
minStack.push(x);
stack.push(x);
}
public void pop() {
if (stack.peek().equals(minStack.peek()))
minStack.pop();
stack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}
class MinStack {
class Node{
int value;
int min;
Node next;
Node(int x, int min){
this.value=x;
this.min=min;
next = null;
}
}
Node head;
public void push(int x) {
if(null==head){
head = new Node(x,x);
}else{
Node n = new Node(x, Math.min(x,head.min));
n.next=head;
head=n;
}
}
public void pop() {
if(head!=null)
head =head.next;
}
public int top() {
if(head!=null)
return head.value;
return -1;
}
public int getMin() {
if(null!=head)
return head.min;
return -1;
}
}
Implement Queue using Queues
class MyStack {
Queue<Integer> q = new LinkedList<Integer>();
public void push(int x) {
q.add(x);
}
public void pop() {
int size = q.size();
for(int i = 1; i < size; i++){
q.add(q.remove());
}
q.remove();
}
public int top() {
int size = q.size();
for(int i = 1; i < size; i++){
q.add(q.remove());
}
int ret = q.remove();
q.add(ret);
return ret;
}
public boolean empty() {
return q.isEmpty();
}
}
Implement Queue Using Stacks
class MyQueue {
Stack<Integer> s1 = new Stack();
Stack<Integer> s2 = new Stack();
public void push(int x) {
while (!s2.isEmpty()){
s1.push(s2.pop());
}
s1.push(x);
}
public void pop() {
while (!s1.isEmpty()){
s2.push(s1.pop());
}
s2.pop();
}
public int peek() {
while (!s1.isEmpty()){
s2.push(s1.pop());
}
return s2.peek();
}
public boolean empty() {
return s1.isEmpty() && s2.isEmpty();
}
}
Basic Calculator
public class Solution {
public int calculate(String s) {
Stack<Integer> stack = new Stack<Integer>();
int result = 0;
int number = 0;
int sign = 1;
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
if(Character.isDigit(c)){
number = 10 * number + (int)(c - '0');
}else if(c == '+'){
result += sign * number;
number = 0;
sign = 1;
}else if(c == '-'){
result += sign * number;
number = 0;
sign = -1;
}else if(c == '('){
stack.push(result);
stack.push(sign);
sign = 1;
result = 0;
}else if(c == ')'){
result += sign * number;
number = 0;
result *= stack.pop();
result += stack.pop();
}
}
if(number != 0) result += sign * number;
return result;
}
}
Largest Rectangle in Histogram
public class Solution {
public int largestRectangleArea(int[] heights) {
if(heights == null) return 0;
if(heights.length == 0) return 0;
int res = 0;
LinkedList<Integer> s = new LinkedList<Integer>();
s.push(-1);
int len = heights.length;
for(int i=0;i<len;i++){
while(s.peek()>-1){
if(heights[s.peek()]>heights[i]){
int top = s.pop();
res = Math.max(res,heights[top]*(i-1-s.peek()));
}else{
break;
}
}
s.push(i);
}
while(s.peek()!=-1){
int top = s.pop();
res = Math.max(res,heights[top]*(len-1-s.peek()));
}
return res;
}
}
Maximal Rectangle
public class Solution {
public int maximalRectangle(char[][] matrix) {
int area = 0, new_area, r, l;
if(matrix.length > 0){
int[] line = new int[matrix[0].length];
boolean[] is_processed = new boolean[matrix[0].length];
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix[i].length; j++){
if (matrix[i][j] == '1') {
line[j]++;
is_processed[j] = false;
} else {
line[j] = 0;
is_processed[j] = true;
}
}
for(int j = 0; j < matrix[i].length; j++){
if(is_processed[j]) continue;
r = l = 1;
while((j + r < line.length)&&(line[j + r] >= line[j])){
if(line[j + r] == line[j]) is_processed[j + r] = true;
r++;
}
while((j - l >= 0)&&(line[j - l] >= line[j])) l++;
new_area = (r + l - 1)*line[j];
if (new_area > area) area = new_area;
}
}
} return area;
}
}