-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinStack.java
More file actions
83 lines (66 loc) · 1.56 KB
/
MinStack.java
File metadata and controls
83 lines (66 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package codingInterview;
import java.util.Arrays;
public class MinStack {
private int MAX_LEN = 100;
private int[] arr;
private int capacity = -1;
public MinStack() {
this.arr = new int[MAX_LEN];
capacity = 0;
}
private void push(int elem) {
if (capacity >= MAX_LEN) {
System.out.println("out of capacity, extend the capacity");
arr = Arrays.copyOf(arr, MAX_LEN * 2);
}
arr[capacity] = elem;
capacity++;
}
private int pop() {
if (capacity <= 0) {
System.out.println("out of capacity");
return Integer.MIN_VALUE;
}
if (capacity == arr.length / 2 && arr.length > MAX_LEN) {
System.out.println("narrow the space of stack");
Arrays.copyOf(arr, arr.length / 2);
}
capacity--;
return arr[capacity];
}
private int top() {
if (capacity <= 0) {
System.out.println("out of range");
return Integer.MIN_VALUE;
}
return arr[capacity - 1];
}
private int getMin() {
if (capacity <= 0 || capacity >= arr.length) {
System.out.println("out of range");
return Integer.MIN_VALUE;
}
int min = Integer.MAX_VALUE;
for (int i = 0; i < capacity; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MinStack stack = new MinStack();
stack.push(5);
stack.push(3);
stack.push(10);
stack.push(0);
for (int i = 0; i < 100; i++) {
stack.push(i);
}
System.out.println(stack.top());
System.out.println(stack.pop());
System.out.println(stack.capacity);
System.out.println(stack.getMin());
}
}