Dynamic Programming Series

Dynamic Programming Series

Day 13

Maximal Square

(Leetcode)

 

Problem Description

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.

Problem Description

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.

Input: 

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

Output: 4

How To approach this problem?

This problem is a classic example of DP on matrix

How To approach this problem?

We can simplify to find the maximum area of square with the given overlapping area.

 

Let's See how?

How To approach this problem?

1 1 0 1 1

1 1 1 1 0

1 1 1 0 1

0 1 1 1 0

0 1 1 1 1 

Focus on square with area 1 only 

How To approach this problem?

1 1 0 1 1

1 1 1 1 0

1 1 1 0 1

0 1 1 1 0

0 1 1 1 1 

Focus on square with area 1 only 

How To approach this problem?

1 1 0 1 1

1 1 1 1 0

1 1 1 0 1

0 1 1 1 0

0 1 1 1 1 

Focus on square with area 1 only 

How To approach this problem?

1 1 0 1 1

1 1 1 1 0

1 1 1 0 1

0 1 1 1 0

0 1 1 1 1 

Focus on square with area 1 only 

How To approach this problem?

1 1 0 1 1

1 1 1 1 0

1 1 1 0 1

0 1 1 1 0

0 1 1 1 1 

Focus on square with area 1 only 

How To approach this problem?

1 1 0 1 1

1 1 1 1 0

1 1 1 0 1

0 1 1 1 0

0 1 1 1 1 

Now Focus on square with size 2 only 

How To approach this problem?

1 1 0 1 1

1 1 1 1 0

1 1 1 0 1

0 1 1 1 0

0 1 1 1 1 

Now Focus on square with size 2 only 

How To approach this problem?

1 1 0 1 1

1 1 1 1 0

1 1 1 0 1

0 1 1 1 0

0 1 1 1 1 

Now Focus on square with size 2 only 

How To approach this problem?

1 1 0 1 1

1 1 1 1 0

1 1 1 0 1

0 1 1 1 0

0 1 1 1 1 

Now Focus on square with size 2 only 

How To approach this problem?

1 1 0 1 1

1 1 1 1 0

1 1 1 0 1

0 1 1 1 0

0 1 1 1 1 

Now Focus on square with size 2 only 

How To approach this problem?

1 1 0 1 1

1 1 1 1 0

1 1 1 0 1

0 1 1 1 0

0 1 1 1 1 

Now Focus on square with size 2 only 

How To approach this problem?

1 1 0 1 1

1 1 1 1 0

1 1 1 0 1

0 1 1 1 0

0 1 1 1 1 

Now Focus on square with size 2 only 

How To approach this problem?

1 1 0 1 1

1 1 1 1 0

1 1 1 0 1

0 1 1 1 0

0 1 1 1 1 

Now Focus on square with size 2 only 

How To approach this problem?

1 1 0 1 1

1 1 1 1 0

1 1 1 0 1

0 1 1 1 0

0 1 1 1 1 

Now Focus on square with size 2 only 

How To approach this problem?

Similarly we can construct more squares of size 2

1 1 0 1 1

1 1 1 1 0

1 1 1 0 1

0 1 1 1 0

0 1 1 1 1 

What are we observing from all these?

?

What are we observing from all these?

3

What are we observing from all these?

?

What are we observing from all these?

2

How To approach this problem?

Apply the same logic for calculating max size square.

1 1 0 1 1

1 1 1 1 0

1 1 1 0 1

0 1 1 1 0

0 1 1 1 1 

How To approach this problem?

Apply the same logic for calculating max size square.

1 1 0 1 1

1 1 1 1 0

1 1 1 0 1

0 1 1 1 0

0 1 1 1 1 

Restriction from here as no square is present

Frame the solution

Now we will frame the solution.

DP[i][j] = min(DP[i-1][j-1], min(DP[i-1][j], DP[i][j-1]))+1
(i-1, j-1) (i-1, j)
(i, j-1) ?

Coding of the solution

int maximalSquare(vector<vector<char>>& matrix) {
    int n = matrix.size();
    if(n == 0)
        return 0;
    int m = matrix[0].size();
    vector <vector<int>> dp(n, vector<int>(m,0));
    int mx = 0;
    for (int i = 0;i < n;i++)
    {
        for (int j = 0;j < m;j++)
        {
            if ((i == 0 || j == 0) && matrix[i][j] == '1')
                dp[i][j] = 1;
            else
            {
                if (matrix[i][j] == '1')
                {
                    dp[i][j] = min(min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1;
                }
            }
            mx = max(mx, dp[i][j]);
        }
    }
    return mx*mx;
}

So that's It for Day 13

So that's It for Day 13

Concepts that we have learnt

So that's It for Day 13

We have taken a very first step in Dynamic Programming.

Concepts that we have learnt

1. DP on Matrix.

So that's It for Day 13

We have taken a very first step in Dynamic Programming.

Concepts that we have learnt

1. DP on Matrix.

2. Use of optimal Substructure with memoization

So that's It for Day 13

We have taken a very first step in Dynamic Programming.

Concepts that we have learnt

1. DP on Matrix.

2. Use of optimal Substructure with memoization

Still, Have any Doubts???

Still, Have any Doubts???

Comment it You will get a reply

OR

Send your doubt to email provided in description.

Copy of Maximal Square

By Chirayu Jain

Copy of Maximal Square

  • 72