forked from Anuj-Kumar-Sharma/Java-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClass.java
More file actions
127 lines (106 loc) · 2.27 KB
/
Copy pathMainClass.java
File metadata and controls
127 lines (106 loc) · 2.27 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package stackDoubts;
import java.util.ArrayDeque;
import java.util.Arrays;
public class MainClass {
public static void main(String[] args) throws Exception {
MinStack obj = new MinStack();
obj.push(5);
obj.push(2);
System.out.println("min" + obj.min());
System.out.println("popped" + obj.pop());
System.out.println("min" + obj.min());
obj.push(9);
obj.push(1);
obj.push(6);
System.out.println("min" + obj.min());
System.out.println("popped" + obj.pop());
System.out.println("min" + obj.min());
System.out.println("popped" + obj.pop());
System.out.println("min" + obj.min());
}
}
class KStacks {
int SIZE = 10;
int k;
int a[], next[], top[], free;
public KStacks(int k) {
this.k = k;
a = new int[SIZE];
next = new int[SIZE];
top = new int[k];
free = 0;
Arrays.fill(top, -1);
for(int i = 0; i<SIZE-1; i++) {
next[i] = i+1;
}
next[SIZE-1] = -1;
}
boolean isFull() {
return free == -1;
}
boolean isStackEmpty(int sn) {
return top[sn] == -1;
}
void push(int data, int sn) throws Exception {
if(isFull()) throw new Exception("Array is Full");
int i = free;
free = next[i];
next[i] = top[sn];
top[sn] = i;
a[i] = data;
}
int pop(int sn) throws Exception {
if(isStackEmpty(sn)) throw new Exception("Stack is Empty");
int i = top[sn];
top[sn] = next[i];
next[i] = free;
free = i;
return a[i];
}
int peek(int sn) throws Exception {
if(isStackEmpty(sn)) throw new Exception("Stack is Empty");
int i = top[sn];
return a[i];
}
}
class MinStack {
ArrayDeque<Integer> stack = new ArrayDeque<>();
private int min = 0;
void push(int t) {
if(stack.isEmpty()) {
min = t;
stack.push(t);
return;
}
if(t >= min) {
stack.push(t);
} else {
stack.push(2*t-min);
min = t;
}
}
int pop() throws Exception {
if(stack.isEmpty()) throw new Exception("Stack is empty");
int t = stack.pop();
if(t >= min) {
return t;
} else {
int ans = min;
min = 2 * min - t;
return ans;
}
}
int min() throws Exception {
if(stack.isEmpty()) throw new Exception("Stack is empty");
return min;
}
int peek() throws Exception {
if(stack.isEmpty()) throw new Exception("Stack is empty");
int t = stack.peek();
if(t >= min) {
return t;
} else {
return min;
}
}
}