-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyStack.java
More file actions
284 lines (252 loc) · 8.05 KB
/
MyStack.java
File metadata and controls
284 lines (252 loc) · 8.05 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
import java.util.*;
public class MyStack {
// static class Node {
// int data;
// Node next;
// Node(int data) {
// this.data = data;
// this.next = null;
// }
// }
// static class Stack {
// static Node head = null;
// public static boolean isLLEmpty() {
// return head == null;
// }
// public static void pushLL(int data) {
// Node newNode = new Node(data);
// if (isLLEmpty()) {
// head = newNode;
// return;
// }
// newNode.next = head;
// head = newNode;
// }
// public static int popLL() {
// if (isLLEmpty()) {
// return -1;
// }
// int top = head.data;
// head = head.next;
// return top;
// }
// public static int peekLL() {
// if (isLLEmpty()) {
// return -1;
// }
// return head.data;
// }
// yaha se arraylist me stack hai line 72 tak
// static ArrayList<Integer> list = new ArrayList<>();
// public static boolean isEmpty() {
// return list.size() == 0;
// }
// public static void push(int data) {
// list.add(data);
// }
// public static int pop() {
// if (isEmpty()) {
// return -1;
// }
// int top = list.get(list.size() - 1);
// list.remove(list.size() - 1);
// return top;
// }
// public static int peek() {
// if (isEmpty()) {
// return -1;
// }
// return list.get(list.size() - 1);
// }
// }
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 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);
pushAtBottom(s, top);
}
public static void printStack(Stack<Integer> s) {
while (!s.isEmpty()) {
System.out.println(s.pop());
}
}
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 nextGreaterElement(int arr[], int nxtGreater[]) { // next smallest nikalna hai to bas while wale
// greater than ki jagah smaller than ka sign
// and equal to ka sign use kar dena hai
// aur curr>=s.peek() kar dena hai
// aur curr<s.peek() kar dena hai
// for loop me i ko 0 se and arr.length-1 tak
// kar dena hai
Stack<Integer> s = new Stack<>();
for (int i = arr.length - 1; i >= 0; i--) {
int curr = arr[i];
while (!s.isEmpty() && curr >= s.peek()) {
s.pop();
}
if (s.isEmpty()) {
nxtGreater[i] = -1;
} else {
nxtGreater[i] = s.peek();
}
s.push(curr);
}
}
public static boolean isValid(String str) {
Stack<Character> s = new Stack<>();
for (int i = 0; i < str.length(); i++) { // time complexity O(n) and space complexity O(n)
char ch = str.charAt(i);
if (ch == '(' || ch == '{' || ch == '[') {
s.push(ch);
} else {
if (s.isEmpty()) { // example str = ")))"
return false;
}
if ((s.peek() == '(' && ch == ')')
|| (s.peek() == '{' && ch == '}')
|| (s.peek() == '[' && ch == ']')) {
s.pop();
} else {
return false;
}
}
}
return s.isEmpty();
}
public static boolean isDuplicate(String str) { // time complexity O(n) and space complexity O(n)
Stack<Character> s = new Stack<>();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
// closing
if (ch == ')') {
int count = 0;
while (s.peek() != '(') {
s.pop();
count++;
}
if (count < 1) {
return true;
} else {
s.pop();
}
} else {
// opening
s.push(ch);
}
}
return false;
}
public static void MaxAreaHistogram(int heights[]) { // time complexity O(n) and space complexity O(n)
int n = heights.length;
int leftSmaller[] = new int[n];
int rightSmaller[] = new int[n];
// left smaller
Stack<Integer> s = new Stack<>();
for (int i = 0; i < n; i++) {
while (!s.isEmpty() && heights[s.peek()] >= heights[i]) {
s.pop();
}
if (s.isEmpty()) {
leftSmaller[i] = -1;
} else {
leftSmaller[i] = s.peek();
}
s.push(i);
}
// right smaller
s = new Stack<>();
for (int i = n - 1; i >= 0; i--) {
while (!s.isEmpty() && heights[s.peek()] >= heights[i]) {
s.pop();
}
if (s.isEmpty()) {
rightSmaller[i] = n;
} else {
rightSmaller[i] = s.peek();
}
s.push(i);
}
// max area
int maxArea = 0;
for (int i = 0; i < n; i++) {
int height = heights[i];
int width = rightSmaller[i] - leftSmaller[i] - 1;
int area = height * width;
maxArea = Math.max(maxArea, area);
}
System.out.println("Max Area in Histogram: " + maxArea);
}
public static void main(String[] args) {
// // Stack s = new Stack();
// Stack<Integer> s = new Stack<>(); // ---> java collection frameworks se bas
// ek line likhna hai then pus pop peek
// // ke alag se function banane ki need nhi
// s.push(0);
// s.push(1);
// s.push(2);
// reverseStack(s);
// printStack(s);
// // String str = "Siddhi";
// // System.out.println(reverseString(str));
int arr[] = { 9, 2, 6, 4, 5 };
int nxtGreater[] = new int[arr.length];
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.print(span[i] + " ");
}
nextGreaterElement(arr, nxtGreater);
for (int i = 0; i < nxtGreater.length; i++) {
System.out.print(nxtGreater[i] + " ");
}
String str = "{[[()]]}";
String str1 = "((a+b))";
int heights[] = { 2, 1, 5, 6, 2, 3 };
MaxAreaHistogram(heights);
System.out.println(isDuplicate(str1));
System.out.println(isValid(str));
}
}