Maximal Square Solution for Google

Company: Google · Difficulty: Medium · Topics: Dynamic Programming, Matrix

Problem Statement

Given an m x n binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.

Key Understanding:

Examples

Example 1:

Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]

Output: 4

Explanation:

```

1 0 1 0 0

1 0 1 1 1

1 1 1 1 1 ← Largest square (2x2)

1 0 0 1 0

```

Example 2:

Input: matrix = [["0","1"],["1","0"]]

Output: 1

Explanation:

```

0 1

1 0 ← Only 1x1 squares possible

```

Example 3:

Input: matrix = [["1","1","1"],["1","1","1"],["1","1","1"]]

Output: 9

Explanation:

```

1 1 1

1 1 1 ← 3x3 square

1 1 1

```

Example 4:

Input: matrix = [["0"]]

Output: 0

Explanation:

Example 5:

Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"],["1","1","1","1","1"]]

Output: 9

Explanation:

```

1 0 1 0 0

1 0 1 1 1

1 1 1 1 1

1 0 0 1 0

1 1 1 1 1 ← 3x3 square

```

Intuition

The key insight is that we can use dynamic programming to build the solution incrementally. For each cell, if it's a 1, the size of the largest square ending at that cell depends on the minimum of the three adjacent cells (top, left, and top-left).

Core Concept:

Why Dynamic Programming Works:

Mathematical Insight:

Approach

Approach 1: Dynamic Programming (Optimal)

Algorithm:

1. Create a DP table of same size as input matrix

2. Initialize first row and column with matrix values

3. For each cell (i,j) where i > 0 and j > 0:

4. Track maximum value in DP table

5. Return maximum value squared

Why This Works:

Complexity Analysis:

Approach 2: Dynamic Programming with Space Optimization

Algorithm:

1. Use a single row to store DP values

2. Keep track of previous row's values

3. Update current row using previous row and current matrix values

4. Track maximum value

5. Return maximum value squared

Why This Works:

Complexity Analysis:

Approach 3: Brute Force

Algorithm:

1. For each cell, try all possible square sizes

2. Check if the square contains only 1's

3. Track the largest valid square found

4. Return the area of largest square

Why This Works:

Complexity Analysis:

Dry Run

Let's trace through Example 1: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]

Dynamic Programming Approach:

Step 1: Initialize DP table

```

Matrix: DP Table:

1 0 1 0 0 1 0 1 0 0

1 0 1 1 1 1 0 1 1 1

1 1 1 1 1 1 1 1 1 1

1 0 0 1 0 1 0 0 1 0

```

Step 2: Fill DP table (starting from row 1, col 1)

Step 3: Final DP table

```

DP Table:

1 0 1 0 0

1 0 1 1 1

1 1 2 2 2

1 0 0 1 0

```

Step 4: Find maximum value

Space Optimized Approach:

Step 1: Initialize

Step 2: Process each row

Step 3: Return maxSide² = 4

Key Insights

1. Optimal Substructure: Each cell depends on three adjacent cells

2. Square Property: A square requires all four corners to be 1's

3. Minimum Rule: Take minimum of three adjacent cells to ensure square

4. Space Optimization: Only need previous row for current computation

5. Edge Cases: Handle first row and column separately

Edge Cases

1. Single Element: Matrix with one element

2. All Zeros: Matrix with no 1's

3. All Ones: Matrix with all 1's

4. Single Row/Column: Matrix with only one row or column

5. Large Matrix: Handle large matrices efficiently

6. Mixed Values: Matrix with both 0's and 1's

Solution

⚠️ Disclaimer: Don't jump directly to the solution, try it out yourself first. Only look at the solution after you've given it a good attempt.

Solution 1: Dynamic Programming (Optimal)

Time Complexity: O(m*n) - We visit each cell once

Space Complexity: O(m*n) - We need a DP table

Advantages:

Disadvantages:

Solution 2: Dynamic Programming with Space Optimization

Time Complexity: O(m*n) - We visit each cell once

Space Complexity: O(n) - We only store one row

Advantages:

Disadvantages:

Solution 3: Brute Force

Time Complexity: O(m*n*min(m,n)²) - For each cell, try all square sizes

Space Complexity: O(1) - Constant extra space

Advantages:

Disadvantages:

More Google coding questions