-
-
Notifications
You must be signed in to change notification settings - Fork 155
Maximal Square Solution (Leetcode) Added problem number 221 #878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
dsa-solutions/lc-solutions/0200-0299/221 - Maximal Square.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
--- | ||
id: Maximal Square | ||
title: Maximal Square | ||
sidebar_label: 221 Maximal Square | ||
tags: | ||
- Dynamic Programming | ||
- Java | ||
- Matrix | ||
- Array | ||
description: "This document provides a solution where we find the largest square containing only 1's and return its area." | ||
--- | ||
|
||
## Problem | ||
|
||
You are 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replace |
||
|
||
### 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 | ||
|
||
**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'$ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replace |
||
|
||
--- | ||
## Approach | ||
There are four approaches discussed that helps to obtain the solution: | ||
|
||
1. **Dynamic Programming Table**: | ||
- Use a 2D DP array **'dp'** where **'dp[i][j]'** represents the side length of the largest square whose bottom-right corner is at position **'(i, j)'**. | ||
|
||
- The value of **'dp[i][j]'** is determined by the values of **'dp[i-1][j]'**, **'dp[i][j-1]'**, and **'dp[i-1][j-1]'**. | ||
|
||
2. **Transition**: | ||
|
||
- If **'matrix[i][j]'** is $'1'$: | ||
- If **'i'** or **'j'** is $0$ (first row or first column), **'dp[i][j]'** is $1$ because the largest square ending there can only be of $size1$. | ||
|
||
- Otherwise, **'dp[i][j]'** is the minimum of **'dp[i-1][j]'**, **'dp[i][j-1]'**, and **'dp[i-1][j-1]'** plus $1$. This is because we can form a larger square only if all three adjacent squares can also form squares of $1's$. | ||
|
||
3. **Max Side Length**: | ||
|
||
- Track the maximum side length of squares found during the iteration. | ||
|
||
4. **Result**: | ||
|
||
- The area of the largest square is the square of the maximum side length found. | ||
|
||
## Solution for Maximal Square | ||
|
||
This problem can be solved using dynamic programming. The problem requires to Utilize a DP table where each entry represents the side length of the largest square ending at that position. | ||
|
||
#### Code in Java | ||
|
||
```java | ||
class Solution { | ||
public int maximalSquare(char[][] matrix) { | ||
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { | ||
return 0; | ||
} | ||
|
||
int rows = matrix.length; | ||
int cols = matrix[0].length; | ||
int maxSide = 0; | ||
|
||
// Create a DP array to store the size of the largest square ending at each position | ||
int[][] dp = new int[rows][cols]; | ||
|
||
// Fill the DP array | ||
for (int i = 0; i < rows; i++) { | ||
for (int j = 0; j < cols; j++) { | ||
if (matrix[i][j] == '1') { | ||
if (i == 0 || j == 0) { | ||
// If we're at the first row or first column, the largest square ending here is just 1 | ||
dp[i][j] = 1; | ||
} else { | ||
// Otherwise, calculate the size of the square based on the surrounding squares | ||
dp[i][j] = Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1; | ||
} | ||
// Update the maximum side length found | ||
maxSide = Math.max(maxSide, dp[i][j]); | ||
} | ||
} | ||
} | ||
|
||
// The area of the largest square is side length squared | ||
return maxSide * maxSide; | ||
} | ||
} | ||
|
||
``` | ||
|
||
### Complexity Analysis | ||
|
||
#### Time Complexity: O($m$ x $n$) | ||
|
||
> **Reason**: The algorithm involves iterating through each cell of the matrix once, leading to a time complexity of $𝑂(𝑚 × 𝑛)$, where $𝑚$ is the number of rows and $𝑛$ is the number of columns. | ||
|
||
#### Space Complexity: O($m$ × $n2$) | ||
|
||
> **Reason**: The space complexity is $𝑂(𝑚 × 𝑛)$ due to the additional DP array used. This could be optimized to $O(n)$ by reusing a single row of DP values, but in the given solution, we use a full 2D DP array. | ||
|
||
# References | ||
|
||
- **LeetCode Problem:** [Maximal Square](https://leetcode.com/problems/maximal-square/description/) | ||
- **Solution Link:** [Maximal Square Solution on LeetCode](https://leetcode.com/problems/maximal-square/solutions/) | ||
- **Authors LeetCode Profile:** [Vivek Vardhan](https://leetcode.com/u/vivekvardhan43862/) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replace
id: Maximal Square
toid: maximal-square