-
Notifications
You must be signed in to change notification settings - Fork 0
/
javastack.java
326 lines (311 loc) · 9.17 KB
/
javastack.java
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import java.util.*;
public class javastack {
public static class Node{
Character data;
Node next;
public Node(Character data){
this.data = data;
this.next = null;
}
}
public static Node head;
public static Node tail;
public static void addFirst(Character data){
Node newNode = new Node(data);
if(head ==null){
head = tail = newNode; return;
}
newNode.next = head;
head = newNode;
}
public static void addLast(Character data){
Node newNode = new Node(data);
if(head == null){
head = tail=newNode;return;
}
tail.next = newNode;
tail= newNode;
}
public static void printLL(){
if(head == null){
System.out.println("Emplty");return;
}
Node temp = head;
while(temp != null){
System.out.println((temp.data));temp = temp.next;
}
System.out.print("null");
}
public static void PushAtBottom(Stack<Integer> s , int data){
if(s.isEmpty()){
s.push(data); return;
}
int top = s.pop();
PushAtBottom(s, data);
s.push(top);
}
public static void revString(String str , Stack<Character>s){
for (int i = 0;i<str.length();i++){
s.push(str.charAt(i));
}
while(!s.isEmpty()){
System.out.print(s.peek());
s.pop();
}
}
public static void PushRevStack(Stack<Integer> s){
if(s.isEmpty()){
return;
}
int top = s.pop();
PushRevStack(s);
PushAtBottom(s, top);
}
public static void PrintStack(Stack<Integer>s){
while(!s.isEmpty()){
System.out.println(s.pop());
}
}
public static void printStocks(int stocks[], Stack<Integer>s,int ans[]){
ans[0]=1;
s.push(0);
for(int i = 1;i<stocks.length;i++){
int currPrice = stocks[i];
while(!s.isEmpty()&&currPrice>stocks[s.peek()]){
s.pop();
}
if(s.isEmpty()){
ans[i]= i+1;
}else{
int prevPriceIndix = s.peek();
ans[i]=i-prevPriceIndix;
}
s.push(i);
}
}
public static void greatestRight(int arr[], Stack<Integer>stack,int ans[]){
ans[arr.length-1]=arr[arr.length-1];
for(int i = arr.length-1;i>=0;i--){
// step 1 --> while loop
while(!stack.isEmpty() && arr[i]>=arr[stack.peek()]){
stack.pop();
}
// step 2 --> if-else
if(stack.isEmpty()){
ans[i]=arr[i];
}
else{
ans[i]= arr[stack.peek()];
}
// step 3 --> Push
stack.push(i);
}
}
public static void smallestRight(int arr[], Stack<Integer>stack,int ans[]){
for(int i = arr.length-1;i>=0;i--){
// step 1 --> while loop
while(!stack.isEmpty() && arr[i]<=arr[stack.peek()]){
stack.pop();
}
// step 2 --> if-else
if(stack.isEmpty()){
ans[i]=-1;
}
else{
ans[i]= arr[stack.peek()];
}
// step 3 --> Push
stack.push(i);
}
}
public static void greatestLeft(int arr[], Stack<Integer>stack,int ans[]){
ans[0] = arr[0];
for(int i = 0;i<arr.length;i++){
// step 1 --> while loop
while(!stack.isEmpty() && arr[i]>=arr[stack.peek()]){
stack.pop();
}
// step 2 --> if-else
if(stack.isEmpty()){
ans[i]=arr[i];
}
else{
ans[i]= arr[stack.peek()];
}
// step 3 --> Push
stack.push(i);
}
}
public static void smallestLeft(int arr[], Stack<Integer>stack,int ans[]){
for(int i = 0;i<arr.length;i++){
// step 1 --> while loop
while(!stack.isEmpty() && arr[i]<=arr[stack.peek()]){
stack.pop();
}
// step 2 --> if-else
if(stack.isEmpty()){
ans[i]=-1;
}
else{
ans[i]= arr[stack.peek()];
}
// step 3 --> Push
stack.push(i);
}
}
public static boolean validParenthese(String str){
Stack<Character> stack = new Stack<>();
for(int i = 0;i<str.length();i++){
Character ch = str.charAt(i);
// Putting all the opening brackets
if(ch=='('||ch=='{'||ch=='['){
stack.push(ch);
}
else{
// checking the stack is not empty
if(stack.isEmpty()){
return false;
}
// checking pairs by matching opening and closing brackets
if((stack.peek()=='('&&ch==')') //()
||(stack.peek()=='['&&ch==']')//[]
||(stack.peek()=='{'&&ch=='}'))//{}
{
stack.pop();
}
else{
return false;
}
}
}
//checking for any element is not left
if(stack.isEmpty()){
return true;
}
else{
return false;
}
}
public static boolean duplicateParenthese(String str){
Stack <Character> stack = new Stack<>();
for(int i = 0 ; i<str.length();i++){
Character ch = str.charAt(i);
if(ch==')'){
int count = 0;
while(stack.peek()!='('){
stack.pop(); count++;
}
if(count<1){
return true;
}else{
stack.pop();
}
}
else{
stack.push(ch);
}
} return false;
}
public static int MaxAreaInHinstrogram(int arr[]){
int max = 0;
Stack<Integer>s = new Stack<>();
int snl[]= new int[arr.length];
int snr[] = new int[arr.length];
//small number left
for(int i =0;i<arr.length;i++){
while(!s.isEmpty()&&arr[s.peek()]>=arr[i]){
s.pop();
}
if(s.isEmpty()){
snl[i]=-1;
}else{
snl[i]= s.peek();
}
s.push(i);
}
//small number right
s = new Stack<>();
for(int i =arr.length-1;i>=0;i--){
while(!s.isEmpty()&&arr[s.peek()]>=arr[i]){
s.pop();
}
if(s.isEmpty()){
snr[i]=arr.length;
}else{
snr[i]= s.peek();
}
s.push(i);
}
//max area
for(int i = 0 ;i<arr.length;i++){
int weight = snr[i]-snl[i]-1; //weight
int height = arr[i];
int currarea = (height * weight);
max = Math.max(max,currarea);
}
return max;
}
public static boolean PaladromeLL1(Node head){
Stack<Character>s = new Stack<>();
boolean result = true;
Node temp = head;
while(temp!=null){
s.push(temp.data);temp = temp.next;
}
while(head!=null){
int i = s.pop();
if(head.data==i){
result = true;
}
else{
result = false;break;
}
head = head.next;
}
return result;
}
public static String SPath(String Path){
Stack<String> stack = new Stack<String>();
StringBuilder res = new StringBuilder();
String[] p = Path.split("/");
for(int i = 0;i<p.length;i++){
if(!stack.isEmpty()&&p[i].equals("..")){
stack.pop();
}
else if(!p[i].equals(".")&&!p[i].equals("")){
stack.push(p[i]);
}
}
if(stack.isEmpty()){
return "/";
}
while(!stack.isEmpty()){
res.insert(0,stack.pop()).insert(0,"/");
}
return res.toString();
}
public static int maxWater(int height[]){
Stack<Integer>stack = new Stack<>();
int n = height.length;
int ans = 0;
for(int i = 0;i<n;i++){
while((!stack.isEmpty())&&(height[stack.peek()]<height[i])){
int popHight = height[stack.pop()];
if(stack.isEmpty()){
break;
}
int distance = i-stack.peek()-1;
int minHeight = Math.min(height[stack.peek()],height[i])-popHight;
ans +=distance*minHeight;
}
stack.push(i);
}
return ans;
}
public static void main(String arg[]){
int height[] = {4,2,0,6,3,2,5};
System.out.println(maxWater(height));
// int arr[]= {2,1,5,6,2,3};
// System.out.print(MaxAreaInHinstrogram(arr));
}
}