using
System;
using
System.Collections;
using
System.Collections.Generic;
public
class
GFG {
public
static
int
[] NextSmallerElement(
int
[, ] M,
int
R,
int
rowIndex)
{
Stack<
int
> st =
new
Stack<
int
>();
st.Push(-1);
int
[] right =
new
int
[R];
for
(
int
i = R - 1; i >= 0; i--) {
if
((st.Peek() != -1)
&& M[rowIndex, st.Peek()]
< M[rowIndex, i]) {
right[i] = st.Peek();
st.Push(i);
}
else
{
while
((st.Peek() != -1)
&& M[rowIndex, st.Peek()]
>= M[rowIndex, i]) {
st.Pop();
}
right[i] = st.Peek();
st.Push(i);
}
}
return
right;
}
public
static
int
[] PreviousSmallerElement(
int
[, ] M,
int
R,
int
rowIndex)
{
Stack<
int
> st =
new
Stack<
int
>();
st.Push(-1);
int
[] left =
new
int
[R];
for
(
int
i = 0; i < R; i++) {
if
((st.Peek() != -1)
&& M[rowIndex, st.Peek()]
< M[rowIndex, i]) {
left[i] = st.Peek();
st.Push(i);
}
else
{
while
((st.Peek() != -1)
&& M[rowIndex, st.Peek()]
>= M[rowIndex, i]) {
st.Pop();
}
left[i] = st.Peek();
st.Push(i);
}
}
return
left;
}
public
static
int
GetMaxArea(
int
[, ] M,
int
R,
int
rowIndex)
{
int
[] right = NextSmallerElement(M, R, rowIndex);
int
[] left = PreviousSmallerElement(M, R, rowIndex);
int
maxarea =
int
.MinValue;
for
(
int
i = 0; i < R; i++) {
int
height = M[rowIndex, i];
if
(right[i] == -1) {
right[i] = R;
}
int
breadth = right[i] - left[i] - 1;
maxarea = Math.Max(maxarea, height * breadth);
}
return
maxarea;
}
public
static
int
MaxRectangleArea(
int
[, ] M,
int
R,
int
C)
{
int
area = GetMaxArea(M, R, 0);
int
maxarea = area;
for
(
int
i = 1; i < R; i++) {
for
(
int
j = 0; j < C; j++) {
if
(M[i, j] != 0) {
M[i, j] = M[i, j] + M[i - 1, j];
}
else
{
M[i, j] = 0;
}
}
maxarea
= Math.Max(maxarea, GetMaxArea(M, R, i));
}
return
maxarea;
}
static
public
void
Main()
{
int
[, ] amt = {
{ 0, 1, 1, 0 },
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 },
{ 1, 1, 0, 0 },
};
int
R = 4, C = 4;
Console.WriteLine(MaxRectangleArea(amt, R, C));
}
}