-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackB.java
More file actions
321 lines (286 loc) · 7.99 KB
/
Copy pathStackB.java
File metadata and controls
321 lines (286 loc) · 7.99 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
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
import java.util.*;
public class StackB {
/*
* static class Node {
* int data;
* Node next;
*
* Node(int data) {
* this.data = data;
* this.next = null;
* }
* }
*
* static class Stack {
*
* Node head = null;
*
* public boolean isEmpty() {
* return head == null;
* }
*
* public void push(int data) {
* Node newNode = new Node(data);
*
* if (isEmpty()) {
* head = newNode;
* return;
* }
*
* newNode.next = head;
* head = newNode;
* }
*
* public int pop() {
* if (isEmpty()) {
* System.out.println("Stack Underflow");
* return -1;
* }
*
* int top = head.data;
* head = head.next;
* return top;
* }
*
* public int peek() {
* if (isEmpty()) {
* System.out.println("Stack Underflow");
* return -1;
* }
*
* return head.data;
* }
* }
*
* // implimentaion using ArrayList
* public static class Stack {
* ArrayList<Integer> list = new ArrayList<>();
*
* public boolean isEmpty() {
* return list.size() == 0;
* }
*
* // push
* public void push(int data) {
* list.add(data);
* }
*
* // pop
* public int pop() {
* if (isEmpty()) {
* return -1;
* }
* int top = list.get(list.size() - 1); // O(1)
* list.remove(list.size() - 1); // O(1)
* return top;
* }
*
* // peak
*
* public int peek() {
* if (isEmpty()) {
* return -1;
* }
* return list.get(list.size() - 1); // O(1)
* }
* }
*/
// push at bottom
public static void pushtAtBottom(Stack<Integer> s, int data) {
// base case
if (s.isEmpty()) {
s.push(data);
return;
}
// recursion
int top = s.pop();
pushtAtBottom(s, data);
s.push(top);
}
public static String reverseString(String str) {
Stack<Character> s = new Stack<>();
int idx = 0;
while (idx < str.length()) {
s.push(str.charAt(idx));
idx++;
}
StringBuilder result = new StringBuilder("");
while (!s.isEmpty()) {
char curr = s.pop();
result.append(curr);
}
return result.toString();
}
public static void reverseStack(Stack<Integer> s) {
if (s.isEmpty()) {
return;
}
int top = s.pop();
reverseStack(s);
pushtAtBottom(s, top);
}
public static void stockSpan(int stocks[], int span[]) {
Stack<Integer> s = new Stack<>();
span[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()) {
span[i] = i + 1;
} else {
int prevHigh = s.peek();
span[i] = i - prevHigh;
}
s.push(i);
}
}
public static void printStack(Stack<Integer> s) {
while (!s.isEmpty()) {
System.out.println(s.pop());
}
}
public static boolean isValid(String str) {
Stack<Character> s = new Stack<>();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == '(' || ch == '{' || ch == '[') { // opening
s.push(ch);
} else {// closing
if (s.isEmpty()) {
return false;
}
if (s.peek() == '(' && ch == ')' ||
s.peek() == '{' && ch == '}' ||
s.peek() == '[' && ch == ']') {
s.pop();
} else {
return false;
}
}
}
if (s.isEmpty()) {
return true;
} else {
return false;
}
}
public static boolean isDuplicate(String str) {
Stack<Character> s = new Stack<>();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
// opening,operator,operand
// closing
if (ch == ')') {
int count = 0;
while (s.peek() != '(') {
// we can directly pop here and then compact the code cause poping here will
// return the value poped
// we are not using "!s.isEmpty && " cause it is already given that it is a
// valid string
s.pop();
count++;
}
if (count < 1) {
return true; // duplicate exists
} else {
s.pop(); // pop opening pair
}
} else {
// opening
s.push(ch);
}
}
return false;
}
public static void maxArea(int arr[]) {
int maxArea = 0;
int nsr[] = new int[arr.length];
int nsl[] = new int[arr.length];
// Next smaller right
Stack<Integer> 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()) {
nsr[i] = arr.length;
} else {
nsr[i] = s.peek();
}
s.push(i);
}
// Next smaller left
s = new Stack<>();
for (int i = 0; i < arr.length; i++) {
while (!s.isEmpty() && arr[s.peek()] >= arr[i]) {
s.pop();
}
if (s.isEmpty()) {
nsl[i] = -1;
} else {
nsl[i] = s.peek();
}
s.push(i);
}
// current area:width=j-i-1=nsr[i]-nsl[i]-1
for (int i = 0; i < arr.length; i++) {
int height = arr[i];
int width = nsr[i] - nsl[i] - 1;
int currArea = height * width;
maxArea = Math.max(currArea, maxArea);
}
System.out.println("Maximum area in histogram = " + maxArea);
}
public static void main(String[] args) {
// Stack s = new Stack();
// Stack<Integer> s = new Stack<>(); // using java colllection framework
// s.push(1);
// s.push(2);
// s.push(3);
// pushtAtBottom(s, 4);
// while (!s.isEmpty()) {
// System.out.println(s.pop());
// }
// String str = "HelloWorld";
// System.out.println(reverseString(str));
// reverseStack(s);
// printStack(s);
// int stocks[] = { 100, 80, 60, 70, 60, 85, 100 };
// int span[] = new int[stocks.length];
// stockSpan(stocks, span);
// for (int i = 0; i < span.length; i++) {
// System.out.println(span[i] + " ");
// }
// int arr[] = { 6, 8, 0, 1, 3 };
// Stack<Integer> s = new Stack<>();
// int nextGreater[] = new int[arr.length];
// for (int i = arr.length - 1; i >= 0; i--) {
// // while
// while (!s.isEmpty() && arr[s.peek()] <= arr[i]) {
// s.pop();
// }
// // if-else
// if (s.isEmpty()) {
// nextGreater[i] = -1;
// } else {
// nextGreater[i] = arr[s.peek()];
// }
// // push in stack
// s.push(i);
// }
// for (int i = 0; i < nextGreater.length; i++) {
// System.out.print(nextGreater[i] + " ");
// }
// System.out.println();
// String str = "({})[]";
// String str1 = "((a+b))"; // true
// String str2 = "(a-b)"; // false
// System.out.println(isDuplicate(str1));
// System.out.println(isDuplicate(str2));
int arr[] = { 2, 1, 5, 6, 2, 3 };// heights in histogram
maxArea(arr);
}
}