Skip to content

Commit f342d1d

Browse files
authored
more STACK based ques
using C++ STL Maximum area histogram (MAH) Maximum area of boolean matrix (MAH variation) Rainwater trapping problem Min stack ques in O(n) space Min stack ques in O(1) space
1 parent 24b1d76 commit f342d1d

File tree

5 files changed

+328
-0
lines changed

5 files changed

+328
-0
lines changed

stack/(10)MinStackO(1).cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// Min stack implementation without using ss stack i.e. in O(1) space
5+
6+
stack<int> s;
7+
int minEle;
8+
9+
void push(int a) {
10+
11+
if (s.size() == 0) { s.push(a); minEle = a; }
12+
13+
else {
14+
15+
if (a >= minEle) {
16+
s.push(a);
17+
}
18+
else {
19+
s.push(2 * a - minEle);
20+
minEle = a;
21+
}
22+
23+
}
24+
return;
25+
}
26+
27+
int pop() {
28+
int ans;
29+
if (s.size() == 0) return -1;
30+
31+
else {
32+
if (s.top() >= minEle) {
33+
ans = s.top();
34+
s.pop();
35+
}
36+
else {
37+
minEle = 2 * minEle - s.top();
38+
ans = s.top();
39+
s.pop();
40+
}
41+
}
42+
43+
return ans;
44+
}
45+
46+
int top() {
47+
48+
if (s.size() == 0) return -1;
49+
50+
else {
51+
if (s.top() <= minEle) return minEle;
52+
else return s.top();
53+
}
54+
}
55+
56+
int getMIN() {
57+
58+
return s.size() == 0 ? -1 : minEle;
59+
}
60+
61+
62+
int main() {
63+
64+
#ifndef ONLINE_JUDGE
65+
freopen("input.txt", "r", stdin);
66+
freopen("output.txt", "w", stdout);
67+
#endif
68+
69+
push(18);
70+
push(19);
71+
push(29);
72+
push(15);
73+
pop();
74+
push(16);
75+
pop();
76+
pop();
77+
78+
cout << getMIN() << endl;
79+
80+
}

stack/(6)MAH.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// Maximum area histogram (nothing to do with cavities) just the rectangle area formed
5+
int main() {
6+
7+
#ifndef ONLINE_JUDGE
8+
freopen("input.txt", "r", stdin);
9+
freopen("output.txt", "w", stdout);
10+
#endif
11+
12+
int a[] = {2, 3, 3, 2};
13+
int n = sizeof(a) / sizeof(int);
14+
15+
vector<int> left;
16+
vector<int> right;
17+
stack<pair<int, int>> s;
18+
stack<pair<int, int>> s1;
19+
20+
// NSL
21+
for (int i = 0; i < n; i++) {
22+
23+
if (s.size() == 0) left.push_back(-1);
24+
else if (s.size() > 0 && s.top().first < a[i]) {
25+
left.push_back(s.top().second);
26+
}
27+
else if (s.size() > 0 && s.top().first >= a[i]) {
28+
while (s.size() > 0 && s.top().first >= a[i]) {
29+
s.pop();
30+
}
31+
if (s.size() == 0) left.push_back(-1);
32+
else left.push_back(s.top().second);
33+
}
34+
s.push({a[i], i});
35+
}
36+
//NSR
37+
for (int i = n - 1; i >= 0; i--) {
38+
39+
if (s1.size() == 0) right.push_back(n);
40+
else if (s1.size() > 0 && s1.top().first < a[i]) {
41+
right.push_back(s1.top().second);
42+
}
43+
else if (s1.size() > 0 && s1.top().first >= a[i]) {
44+
while (s1.size() > 0 && s1.top().first >= a[i]) {
45+
s1.pop();
46+
}
47+
if (s1.size() == 0) right.push_back(n);
48+
else right.push_back(s1.top().second);
49+
}
50+
s1.push({a[i], i});
51+
}
52+
reverse(right.begin(), right.end());
53+
54+
int area = INT_MIN;
55+
int temp;
56+
for (int i = 0; i < n; i++) {
57+
temp = (right[i] - left[i] - 1) * a[i];
58+
area = max(area, temp);
59+
}
60+
cout << area;
61+
62+
}

stack/(7)MaxBM.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// Maximum area of rectangle in a binary matrix
5+
int MAH(vector<int> a, int n) {
6+
vector<int> left;
7+
vector<int> right;
8+
stack<pair<int, int>> s;
9+
stack<pair<int, int>> s1;
10+
11+
// NSL
12+
for (int i = 0; i < n; i++) {
13+
14+
if (s.size() == 0) left.push_back(-1);
15+
else if (s.size() > 0 && s.top().first < a[i]) {
16+
left.push_back(s.top().second);
17+
}
18+
else if (s.size() > 0 && s.top().first >= a[i]) {
19+
while (s.size() > 0 && s.top().first >= a[i]) {
20+
s.pop();
21+
}
22+
if (s.size() == 0) left.push_back(-1);
23+
else left.push_back(s.top().second);
24+
}
25+
s.push({a[i], i});
26+
}
27+
//NSR
28+
for (int i = n - 1; i >= 0; i--) {
29+
30+
if (s1.size() == 0) right.push_back(n);
31+
else if (s1.size() > 0 && s1.top().first < a[i]) {
32+
right.push_back(s1.top().second);
33+
}
34+
else if (s1.size() > 0 && s1.top().first >= a[i]) {
35+
while (s1.size() > 0 && s1.top().first >= a[i]) {
36+
s1.pop();
37+
}
38+
if (s1.size() == 0) right.push_back(n);
39+
else right.push_back(s1.top().second);
40+
}
41+
s1.push({a[i], i});
42+
}
43+
reverse(right.begin(), right.end());
44+
45+
int area = INT_MIN;
46+
int temp;
47+
for (int i = 0; i < n; i++) {
48+
temp = (right[i] - left[i] - 1) * a[i];
49+
area = max(area, temp);
50+
}
51+
return area;
52+
}
53+
54+
int main() {
55+
56+
57+
#ifndef ONLINE_JUDGE
58+
freopen("input.txt", "r", stdin);
59+
freopen("output.txt", "w", stdout);
60+
#endif
61+
62+
int n;
63+
cin >> n;
64+
int a[n][n];
65+
66+
vector<int> v;
67+
68+
for (int i = 0; i < n; i++) {
69+
for (int j = 0; j < n; j++) {
70+
cin >> a[i][j];
71+
}
72+
}
73+
74+
for (int j = 0; j < n; j++) {
75+
v.push_back(a[0][j]);
76+
}
77+
78+
int ans = MAH(v, n);
79+
80+
for (int i = 1; i < n; i++) {
81+
for (int j = 0; j < n; j++) {
82+
if (a[i][j] == 0) v[j] = 0;
83+
else v[j] = v[j] + a[i][j];
84+
}
85+
ans = max(ans, MAH(v, n));
86+
}
87+
88+
cout << ans;
89+
90+
}

stack/(8)RainwaterTrap.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// Rainwater trapping (total trapped water)
5+
6+
int main() {
7+
8+
#ifndef ONLINE_JUDGE
9+
freopen("input.txt", "r", stdin);
10+
freopen("output.txt", "w", stdout);
11+
#endif
12+
13+
int n;
14+
cin >> n;
15+
int a[n];
16+
17+
for (int i = 0; i < n; i++) {
18+
cin >> a[i];
19+
}
20+
21+
int maxL[n];
22+
int maxR[n];
23+
24+
maxL[0] = a[0];
25+
maxR[n - 1] = a[n - 1];
26+
27+
for (int i = 1; i < n; i++) {
28+
maxL[i] = max(maxL[i - 1], a[i]);
29+
}
30+
31+
for (int i = n - 2; i >= 0; i--) {
32+
maxR[i] = max(maxR[i + 1], a[i]);
33+
}
34+
35+
int ans = 0;
36+
for (int i = 0; i < n; i++) {
37+
ans = ans + min(maxL[i], maxR[i]) - a[i];
38+
}
39+
40+
cout << ans << endl;
41+
42+
}

stack/(9)MinStackO(n).cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// Min stack implementation using ss stack
5+
6+
stack<int> s;
7+
stack<int> ss; //supporting stack
8+
9+
void push(int a) {
10+
11+
s.push(a);
12+
if (ss.size() == 0 || a <= ss.top()) {
13+
ss.push(a);
14+
}
15+
return;
16+
}
17+
18+
int pop() {
19+
20+
int val = s.top();
21+
s.pop();
22+
23+
if (ss.top() == val) {
24+
ss.pop();
25+
}
26+
27+
return val;
28+
}
29+
30+
int getMIN() {
31+
32+
return ss.size() == 0 ? -1 : ss.top();
33+
}
34+
35+
36+
int main() {
37+
38+
#ifndef ONLINE_JUDGE
39+
freopen("input.txt", "r", stdin);
40+
freopen("output.txt", "w", stdout);
41+
#endif
42+
43+
push(18);
44+
push(19);
45+
push(29);
46+
push(15);
47+
pop();
48+
push(16);
49+
pop();
50+
pop();
51+
52+
cout << getMIN() << endl;
53+
54+
}

0 commit comments

Comments
 (0)