Skip to content

Commit e09140a

Browse files
committed
added problem of stack
1 parent 3ce4648 commit e09140a

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

Stack/MaxRectangle.java

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package Stack;
2+
3+
public class MaxRectangle {
4+
5+
public static int[] nextsmallerelement(int[] arr, int n)
6+
{
7+
8+
Stack<Integer> st = new Stack<>();
9+
10+
// For the elements which dont have
11+
// next smaller element ans will be -1
12+
st.push(-1);
13+
14+
// Store indices in output
15+
int[] right = new int[n];
16+
17+
// Start from last index
18+
for (int i = n - 1; i >= 0; i--) {
19+
20+
// If top element is sorted then
21+
// no need to do anything, just store
22+
// the answer and push the
23+
// current element in stack
24+
if ((st.peek() != -1)
25+
&& arr[st.peek()] < arr[i]) {
26+
right[i] = st.peek();
27+
st.push(i);
28+
}
29+
else {
30+
while ((st.peek() != -1)
31+
&& arr[st.peek()]
32+
>= arr[i]) {
33+
st.pop();
34+
}
35+
right[i] = st.peek();
36+
st.push(i);
37+
}
38+
}
39+
return right;
40+
}
41+
42+
public static int[] previousmallerelement(int arr[],int n)
43+
{
44+
Stack<Integer> st = new Stack<>();
45+
st.push(-1);
46+
47+
int[] left = new int[n];
48+
49+
// Start from first index - Difference Between Next and Previous Smaller element
50+
for (int i = 0; i < n; i++) {
51+
if ((st.peek() != -1)
52+
&& arr[st.peek()] < arr[i]) {
53+
left[i] = st.peek();
54+
st.push(i);
55+
}
56+
else {
57+
while ((st.peek() != -1)
58+
&& arr[st.peek()]
59+
>= arr[i]) {
60+
st.pop();
61+
}
62+
left[i] = st.peek();
63+
st.push(i);
64+
}
65+
}
66+
return left;
67+
}
68+
public static int getMaxArea(int [] arr, int n)
69+
{
70+
int [] right = new int [n];
71+
right = nextsmallerelement(arr, n);
72+
73+
// Find the smallest element than
74+
// curr element in right side
75+
76+
int [] left = new int [n];
77+
left = previousmallerelement(arr, n);
78+
79+
// Find the smallest element
80+
// than curr element in left side
81+
int maxarea = Integer.MIN_VALUE;
82+
83+
// Now the left and right array have
84+
// index of smallest element in left and
85+
// right respectively, thus the difference
86+
// of right - left - 1 will give us
87+
// breadth and thus
88+
// area = height(curr==arr[i]) * breadth;
89+
for (int i = 0; i < n; i++) {
90+
int height = arr[i];
91+
if (right[i] == -1) {
92+
right[i] = n;
93+
}
94+
int breadth = right[i] - left[i] - 1;
95+
maxarea = Math.max(maxarea,
96+
height * breadth);
97+
}
98+
return maxarea;
99+
}
100+
101+
public static int maxRectangleArea(int [][] M, int R, int C)
102+
{
103+
int area = getMaxArea(M[0], R);
104+
int maxarea = area;
105+
106+
for (int i = 1; i < R; i++) {
107+
for (int j = 0; j < C; j++) {
108+
if (M[i][j] != 0) {
109+
110+
// Add heights of previous rows
111+
// into current
112+
M[i][j] = M[i][j]
113+
+ M[i - 1][j];
114+
}
115+
else {
116+
117+
// If current height is 0 then
118+
// don't add previous heights
119+
M[i][j] = 0;
120+
}
121+
}
122+
maxarea = Math.max(maxarea,
123+
getMaxArea(M[i], R));
124+
}
125+
return maxarea;
126+
}
127+
public static void main(String[] args) {
128+
int R = 4, C = 4;
129+
int [][]amt = {
130+
{ 0, 1, 1, 0 },
131+
{ 1, 1, 1, 1 },
132+
{ 1, 1, 1, 1 },
133+
{ 1, 1, 0, 0 },
134+
};
135+
System.out.println(maxRectangleArea(amt, R, C));
136+
}
137+
}

0 commit comments

Comments
 (0)