> For the complete documentation index, see [llms.txt](https://soumyajit4419.gitbook.io/ds-algo/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://soumyajit4419.gitbook.io/ds-algo/stack-and-queue/applications/12.-maximal-rectangle.md).

# 13. Maximal Rectangle

Given a `rows x cols` binary `matrix` filled with `0`'s and `1`'s, find the largest rectangle containing only `1`'s and return *its area*.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/09/14/maximal.jpg)

```
Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
Output: 6
Explanation: The maximal rectangle is shown in the above picture.
```

```
Example 2:
Input: matrix = []
Output: 0


Example 3:
Input: matrix = [["0"]]
Output: 0


Example 4:
Input: matrix = [["1"]]
Output: 1


Example 5:
Input: matrix = [["0","0"]]
Output: 0
```

## Solution: (Using Stack)

**Approach:** \
**Same as Largest Rectangle in a Histogram Problem**\
**Computing largest rectangle in each row**&#x20;

```cpp
class Solution
{
public:
    int maximalRectangle(vector<vector<char>> &matrix)
    {

        int n = matrix.size();
        if(n == 0){
            return 0;
        }
        int m = matrix[0].size();

        vector<vector<int>> v(n + 1, vector<int>(m, 0));

        for (int i = 1; i <= n; i++)
        {
            for (int j = 0; j < m; j++)
            {

                if (matrix[i - 1][j] == '1')
                {
                    v[i][j] = 1 + v[i - 1][j];
                }
                else
                {
                    v[i][j] = 0;
                }
            }
        }

        int maxArea = INT_MIN;

        for (int i = 1; i <= n; i++)
        {
            stack<int> ls;
            stack<int> rs;
            vector<int> lm(m);
            vector<int> rm(m);

            // Left Limit
            for (int j = 0; j < m; j++)
            {

                if (ls.empty())
                {
                    lm[j] = 0;
                    ls.push(j);
                }
                else if (!ls.empty() && v[i][ls.top()] < v[i][j])
                {
                    lm[j] = ls.top() + 1;
                    ls.push(j);
                }
                else
                {
                    while (!ls.empty() && v[i][ls.top()] >= v[i][j])
                    {
                        ls.pop();
                    }

                    if (ls.empty())
                    {
                        lm[j] = 0;
                    }
                    else
                    {
                        lm[j] = ls.top() + 1;
                    }
                    ls.push(j);
                }
            }

            // Right Limit
            for (int j = m - 1; j >= 0; j--)
            {

                if (rs.empty())
                {
                    rm[j] = m - 1;
                    rs.push(j);
                }
                else if (!rs.empty() && v[i][rs.top()] < v[i][j])
                {
                    rm[j] = rs.top() - 1;
                    rs.push(j);
                }
                else
                {
                    while (!rs.empty() && v[i][rs.top()] >= v[i][j])
                    {
                        rs.pop();
                    }

                    if (rs.empty())
                    {
                        rm[j] = m - 1;
                    }
                    else
                    {
                        rm[j] = rs.top() - 1;
                    }
                    rs.push(j);
                }
            }

            for (int k = 0; k < rm.size(); k++)
            {
                int area = (rm[k] - lm[k] + 1) * v[i][k];
                if (area > maxArea)
                {
                    maxArea = area;
                }
            }
        }
        
        return maxArea;
    }
};

```

**Time Complexity: O(n \* m)**\
**Space Compexity: O(n + m)**


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://soumyajit4419.gitbook.io/ds-algo/stack-and-queue/applications/12.-maximal-rectangle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
