Maximal Square - Complete Solution Guide
Maximal Square is LeetCode problem 221, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
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 . 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 Example 2: Input: matrix = [["0","1"],["1","0"]] Output: 1 Example 3: Input: matrix = [["0"]] Output: 0 Constraints: m == matrix.length n == matrix[i].length 1 <= m, n <= 300 matrix[i][j] is '0' or '1' .
Detailed Explanation
The problem asks us to find the largest square submatrix within a given binary matrix (containing only '0's and '1's) that contains only '1's. We need to return the area of this largest square.
Solution Approach
The solution employs dynamic programming to build a DP table (dp) where dp[i][j] represents the side length of the largest square ending at matrix[i-1][j-1]. We iterate through the matrix, and if matrix[i-1][j-1] is '1', we calculate dp[i][j] using the formula mentioned above (Insight 2). We keep track of the maximum side length encountered, and finally return its square as the area.
Step-by-Step Algorithm
- Step 1: Initialize a DP table `dp` of size (m+1) x (n+1) with all values set to 0. This table will store the side length of the largest square ending at each cell.
- Step 2: Initialize `max_side` to 0. This variable will store the side length of the largest square found so far.
- Step 3: Iterate through the input `matrix` from row 1 to m and column 1 to n (using `i` and `j` as indices).
- Step 4: Inside the inner loop, check if `matrix[i-1][j-1]` is equal to '1'.
- Step 5: If `matrix[i-1][j-1]` is '1', calculate `dp[i][j]` as 1 + min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]). This uses the dynamic programming relation to determine the size of the square ending at the current cell.
- Step 6: Update `max_side` with the maximum value between the current `max_side` and `dp[i][j]`.
- Step 7: After the loops finish, return `max_side * max_side`. This returns the area of the largest square.
Key Insights
- Insight 1: Dynamic programming is well-suited because the size of the largest square ending at a given cell depends on the sizes of the largest squares ending at its top, left, and top-left neighbors.
- Insight 2: The side length of the maximal square ending at matrix[i][j] is 1 + min(side length of square ending at matrix[i-1][j], side length of square ending at matrix[i][j-1], side length of square ending at matrix[i-1][j-1]).
- Insight 3: The area is simply the square of the side length of the largest square.
Complexity Analysis
Time Complexity: O(m*n)
Space Complexity: O(m*n)
Topics
This problem involves: Array, Dynamic Programming, Matrix.
Companies
Asked at: Airbnb, BharatPe, Booking.com, ByteDance, Citadel, DE Shaw, GSA Capital, Goldman Sachs, Karat, Myntra, Nvidia, Oracle, PayPal, PhonePe, SAP, Salesforce, ServiceNow, Walmart Labs, Wise, eBay.