Repository navigation
Expand file tree
/
Copy pathLearnStack.java
More file actions
253 lines (226 loc) · 6.03 KB
/
Copy pathLearnStack.java
File metadata and controls
253 lines (226 loc) · 6.03 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
import java.util.ArrayList;
import java.util.Stack;
class LearnStack{
public boolean isValid(String s) {
Stack<Character>st = new Stack<>();
for(int i=0;i<s.length();i++)
{
char ch = s.charAt(i);
if(ch=='{' || ch=='[' || ch=='(')
st.push(ch);
else //Closing Bracket
{
if(st.size()==0)
return false;
else
{
char ch2 = st.peek();
if(ch=='}' && ch2=='{')
st.pop();
else if(ch==']' && ch2=='[')
st.pop();
else if(ch==')' && ch2=='(')
st.pop();
else
return false;
}
}
}
if(st.size()==0)
return true;
return false;
}
public int[] NGOR(int []arr)
{
Stack<Integer> st = new Stack<>();
int []ans = new int[arr.length];
for(int i = arr.length-1;i>=0;i--)
{
//REMOVE ALL SMALLER ELEMENT
while(st.size()>0 && st.peek()<=arr[i])
st.pop();
if(st.size()==0)
ans[i]=-1;
else
ans[i]=st.peek();
st.push(arr[i]);
}
return ans;
}
public int[] NSOR(int []arr)
{
Stack<Integer> st = new Stack<>();
int []ans = new int[arr.length];
for(int i = arr.length-1;i>=0;i--)
{
//REMOVE ALL GREATER ELEMENT
while(st.size()>0 && st.peek()>=arr[i])
st.pop();
if(st.size()==0)
ans[i]=-1;
else
ans[i]=st.peek();
st.push(arr[i]);
}
return ans;
}
public int[] NSOL(int []arr)
{
Stack<Integer> st = new Stack<>();
int []ans = new int[arr.length];
// for(int i = arr.length-1;i>=0;i--)
for(int i = 0;i<arr.length;i++)
{
//REMOVE ALL GREATER ELEMENT
while(st.size()>0 && st.peek()>=arr[i])
st.pop();
if(st.size()==0)
ans[i]=-1;
else
ans[i]=st.peek();
st.push(arr[i]);
}
return ans;
}
// NGOR NGOL NSOL NSOR VV IMPORTANT
public ArrayList<Integer> calculateSpan(int[] arr) {
// code here
ArrayList<Integer>fans = new ArrayList<>();
Stack<Integer>st = new Stack<>();
int []ans = new int[arr.length];
//NGOL
for(int i=0;i<arr.length;i++)
{
//Smaller Element Remove
while(st.size()>0 && arr[i]>=arr[st.peek()])
st.pop();
if(st.size()==0)
ans[i]=-1;
else
ans[i]=st.peek();
st.push(i);
}
for(int i=0;i<ans.length;i++)
{
fans.add(i-ans[i]);
}
return fans;
}
public int[] NSOL_helper(int []arr)
{
int []ans = new int[arr.length];
Stack<Integer> st = new Stack<>();
for(int i=0;i<arr.length;i++)
{
while(st.size()>0 && arr[i]<=arr[st.peek()])
st.pop();
if(st.size()==0)
ans[i]=-1;
else
ans[i]=st.peek();
st.push(i);
}
return ans;
}
public int[] NSOR_helper(int []arr)
{
int []ans = new int[arr.length];
Stack<Integer> st = new Stack<>();
// for(int i=0;i<arr.length;i++)
for(int i=arr.length-1;i>=0;i--)
{
while(st.size()>0 && arr[i]<=arr[st.peek()])
st.pop();
if(st.size()==0)
ans[i]=arr.length;
else
ans[i]=st.peek();
st.push(i);
}
return ans;
}
public int largestRectangleArea(int[] arr) {
int []nsol = NSOL_helper(arr);
int []nsor = NSOR_helper(arr);
int maxArea = 0;
for(int i=0;i<arr.length;i++)
{
int width = nsor[i]-nsol[i]-1;
int height = arr[i];
int curr_area = width*height;
if(curr_area>maxArea)
maxArea=curr_area;
}
return maxArea;
}
public int longestValidParentheses(String s) {
int ob = 0;
int cb = 0;
int max_Score = 0;
int curr_Score = 0;
for(int i=0;i<s.length();i++)
{
char ch = s.charAt(i);
if(ch=='(')
ob++;
else
cb++;
if(ob == cb)
{
curr_Score = ob+cb;
}
max_Score = Math.max(max_Score,curr_Score);
if(cb>ob)
{
cb = 0;
ob = 0;
}
}
ob = 0;
cb = 0;
for(int i=s.length()-1;i>=0;i--)
{
char ch = s.charAt(i);
if(ch=='(')
ob++;
else
cb++;
if(ob == cb)
{
curr_Score = ob+cb;
}
max_Score = Math.max(max_Score,curr_Score);
// if(cb>ob)
if(ob>cb)
{
cb = 0;
ob = 0;
}
}
return max_Score;
}
public static void run()
{
Stack<Integer>st = new Stack<>();
st.push(10);
st.push(20);
st.push(30);
System.out.println(st.peek());
System.out.println(st.pop());
System.out.println(st.peek());
System.out.println(st.size());
st.clear();
if(st.isEmpty())
{
System.out.println("Stack is Empty");
}
else
{
System.out.println("Stack is not Empty");
}
System.out.println(st.size());
}
public static void main(String[] args) {
run();
}
}