What's the most efficient algorithm to find the rectangle with the largest area which will fit in the empty space?

Let's say the screen looks like this ('#' represents filled area):

```
....................
..............######
##..................
.................###
.................###
#####...............
#####...............
#####...............
```

A probable solution is:

```
....................
..............######
##...++++++++++++...
.....++++++++++++###
.....++++++++++++###
#####++++++++++++...
#####++++++++++++...
#####++++++++++++...
```

Normally I'd enjoy figuring out a solution. Although this time I'd like to avoid wasting time fumbling around on my own since this has a practical use for a project I'm working on. Is there a well-known solution?

**Shog9** wrote:

Is your input an array (as implied by the other responses), or a list of occlusions in the form of arbitrarily sized, positioned rectangles (as might be the case in a windowing system when dealing with window positions)?

Yes, I have a structure which keeps track of a set of windows placed on the screen. I also have a grid which keeps track of all the areas between each edge, whether they are empty or filled, and the pixel position of their left or top edge. I think there is some modified form which would take advantage of this property. Do you know of any?

I'm the author of that Dr. Dobb's article and get occasionally asked about an implementation. Here is a simple one in C:

It takes its input from the console. You could e.g. pipe this file to it:

And after printing its input, it will output:

The implementation above is nothing fancy of course, but it's very close to the explanation in the Dr. Dobb's article and should be easy to translate to whatever is needed.

@lassevk

I found the referenced article, from DDJ: The Maximal Rectangle Problem

Here's a page that has some code and some analysis.

Your particular problem begins a bit down on the page, search the page for the text

maximal rectangle problem.http://www.seas.gwu.edu/~simhaweb/cs151/lectures/module6/module6.html

@lassevk

HAHA... O(m2 n2).. That's probably what I would have come up with.

I see they go on to develop optmizations... looks good, I'll have a read.

I implemented the solution of Dobbs in Java.

No warranty for anything.

After struggling so much I've wrote this algorithm...Just see the code...

You understand that.This code speaks !!

I am the author of the Maximal Rectangle Solution on LeetCode, which is what this answer is based on.

Since the stack-based solution has already been discussed in the other answers, I would like to present an optimal

`O(NM)`

dynamic programming solution which originates from user morrischen2008.IntuitionImagine an algorithm where for each point we computed a rectangle by doing the following:

Finding the maximum height of the rectangle by iterating upwards until a filled area is reached

Finding the maximum width of the rectangle by iterating outwards left and right until a height that doesn't accommodate the maximum height of the rectangle

For example finding the rectangle defined by the yellow point:

We know that the maximal rectangle must be one of the rectangles constructed in this manner (the max rectangle must have a point on its base where the next filled square is

heightabove that point).For each point we define some variables:

`h`

- the height of the rectangle defined by that point`l`

- the left bound of the rectangle defined by that point`r`

- the right bound of the rectangle defined by that pointThese three variables uniquely define the rectangle at that point. We can compute the area of this rectangle with

`h * (r - l)`

. The global maximum of all these areas is our result.Using dynamic programming, we can use the

`h`

,`l`

, and`r`

of each point in the previous row to compute the`h`

,`l`

, and`r`

for every point in the next row in linear time.AlgorithmGiven row

`matrix[i]`

, we keep track of the`h`

,`l`

, and`r`

of each point in the row by defining three arrays -`height`

,`left`

, and`right`

.`height[j]`

will correspond to the height of`matrix[i][j]`

, and so on and so forth with the other arrays.The question now becomes how to update each array.

`h`

is defined as the number of continuous unfilled spaces in a line from our point. We increment if there is a new space, and set it to zero if the space is filled (we are using '1' to indicate an empty space and '0' as a filled one).Consider what causes changes to the left bound of our rectangle. Since all instances of filled spaces occurring in the row above the current one have already been factored into the current version of

`left`

, the only thing that affects our`left`

is if we encounter a filled space in our current row.As a result we can define:

`cur_left`

is one greater than rightmost filled space we have encountered. When we "expand" the rectangle to the left, we know it can't expand past that point, otherwise it'll run into the filled space.Here we can reuse our reasoning in

`left`

and define:`cur_right`

is the leftmost occurrence of a filled space we have encountered.Implementation