Maximal Square and Rectangle

Published: Jun 16, 2017

A bunch of algorithm questions take a style of “maximum is a good thing.” Maximal sum, maximal length or maximal size are examples. This memo is about maximal size, precisely, square and rectangle.

These two have quite similar descriptions. So, I call those siblings. The problems are “given 2D matrix filled with 0’s and 1’s, find maximal square/rectangle.” Approaches how to solve are also similar. However, a difficulty level is not the same. The maximal square question is much easier. The maximal rectangle question needs an additional step to find the maximum.

I’m going to start off with the maximal square.

Problem Description - Maximal Square

Given a 2D binary matrix filled with 0’s and 1’s, find the maximal square with all 1’s.

For example, following 2D matrix is given:


1   0   1   0   0

1   0   1   1   1

1   1   1   1   1

1   0   0   1   0

The answer will be 4.


1   0   1   0   0
      +-------+
1   0 | 1   1 | 1
      |       |
1   1 | 1   1 | 1
      +-------+
1   0   0   1   0

The idea to find maximal square

This is a dynamic programming question, so optimal substructure exists:

  1. include the current cell to form a square
  2. exclude the current call

The state to maintain in the auxiliary table is the size of the square so far. Following the DP manner, the table will be created by bottom up.

The first column and row remain as those are. When the value of matrix at i’th row and j’th column is 1, look above, above-left, and left. Among three, find minimum then plus one. This is the value in the auxiliary table.

Java code for finding maximal square

The performance is: time O(nm), space O(nm), where n: rows, m: columns

The result is:


4

Problem Description - Maximal Rectangle

Given a 2D binary matrix filled with 0’s and 1’s, find the maximal rectangle with all 1’s.

For example, following 2D matrix is given:


1   0   1   0   0

1   0   1   1   1

1   1   1   1   1

1   0   0   1   0

The answer will be 6.


1   0   1   0   0
      +-----------+
1   0 | 1   1   1 |
      |           |
1   1 | 1   1   1 |
      +-----------+
1   0   0   1   0

The idea to find maximal rectangle

Also, this is a dynamic programming problem, but has an extra process. The first step of DP sees vertically. The second step sees horizontally.

For the DP step, the optimal substructure exists, like the square problem.

  1. include the current cell to form a rectangle
  2. exclude the current cell

The state to maintain in the auxiliary table is the size of 1’s of vertical stretch. Following the DP manner, the table will be created by bottom up. When the value of matrix at i’th row and j’th column is 1, look above then plus one. This is the value in the auxiliary table.

After creating the auxiliary table, I need to find the maximal area looking at horizontally. How to find it? This is exactly the same as Largest Rectangle in Histogram problem.

The second step looks each row to find its max. By comparison of each max, I can get the maximal area.

Java code for finding maximal rectangle

The performance is: time O(nm), space O(nm), where n: rows, m: columns

The result is:


6

Resources

Latest Posts

Vite + Vue + Bun on Rails

Vue.js is one of frontend frameworks gaining popularity among rapidly emerging JavaScript technologies. The combination of Vue.js and Rails is becoming more popular as well, however, Vue.js development on Rails is not so straightforward. The reason would be that Vue.js relies on Vite for a development environment such as HMR (Hot Module Replacement) and bundling.

Bun + React on Rails

In the frontend world, new technologies keep emerging rapidly these years. Still, React is a well-established and very popular frontend framework, Vue.js, Svelte, Astro and more frameworks are gaining popularity. Not just the frameworks, tools for a transpiler/bundler or sort are also under a rapid development. In JavaScript domain, esbuild, rollup, vite and some more are out.

Ruby on Rails Secrets Management

A web application needs various kinds of values, params and etc which should not be revealed to the public, say GitHub repo. For example, API keys, tokens, passwords, and endpoints, all those should be kept secret. Another important factor is that such secrets should be shared among the team members. Additionally, all those secrets should come along with the deployment.