Skip to content

Commit ce29da2

Browse files
authored
Merge pull request #878 from vivekvardhan2810/main
Maximal Square Solution (Leetcode) Added problem number 221
2 parents d32750f + e1aa16c commit ce29da2

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
id: maximal-square
3+
title: Maximal Square
4+
sidebar_label: 221 Maximal Square
5+
tags:
6+
- Dynamic Programming
7+
- Java
8+
- Matrix
9+
- Array
10+
description: "This document provides a solution where we find the largest square containing only 1's and return its area."
11+
---
12+
13+
## Problem
14+
15+
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.
16+
17+
### Examples
18+
19+
**Example 1:**
20+
21+
![image](https://github.com/vivekvardhan2810/codeharborhub.github.io/assets/91594529/4816deda-e3ba-47d4-b6d8-b38dd4fe67e2)
22+
23+
**Input:** matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
24+
25+
**Output:** 4
26+
27+
**Example 2:**
28+
29+
![image](https://github.com/vivekvardhan2810/codeharborhub.github.io/assets/91594529/43f9556f-bdde-4d5c-b8ab-a5807c51cb3c)
30+
31+
**Input:** matrix = [["0","1"],["1","0"]]
32+
33+
**Output:** 1
34+
35+
**Example 3:**
36+
37+
**Input:** matrix = [["0"]]
38+
39+
**Output:** 0
40+
41+
### Constraints
42+
43+
- $m == matrix.length$
44+
- $n == matrix[i].length$
45+
- $1 <= m, n <= 300$
46+
- $matrix[i][j]$ is $0$ or $1$
47+
48+
---
49+
## Approach
50+
There are four approaches discussed that helps to obtain the solution:
51+
52+
1. **Dynamic Programming Table**:
53+
- 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)'**.
54+
55+
- 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]'**.
56+
57+
2. **Transition**:
58+
59+
- If **'matrix[i][j]'** is $1$:
60+
- 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$.
61+
62+
- 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$.
63+
64+
3. **Max Side Length**:
65+
66+
- Track the maximum side length of squares found during the iteration.
67+
68+
4. **Result**:
69+
70+
- The area of the largest square is the square of the maximum side length found.
71+
72+
## Solution for Maximal Square
73+
74+
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.
75+
76+
#### Code in Java
77+
78+
```java
79+
class Solution {
80+
public int maximalSquare(char[][] matrix) {
81+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
82+
return 0;
83+
}
84+
85+
int rows = matrix.length;
86+
int cols = matrix[0].length;
87+
int maxSide = 0;
88+
89+
// Create a DP array to store the size of the largest square ending at each position
90+
int[][] dp = new int[rows][cols];
91+
92+
// Fill the DP array
93+
for (int i = 0; i < rows; i++) {
94+
for (int j = 0; j < cols; j++) {
95+
if (matrix[i][j] == '1') {
96+
if (i == 0 || j == 0) {
97+
// If we're at the first row or first column, the largest square ending here is just 1
98+
dp[i][j] = 1;
99+
} else {
100+
// Otherwise, calculate the size of the square based on the surrounding squares
101+
dp[i][j] = Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1;
102+
}
103+
// Update the maximum side length found
104+
maxSide = Math.max(maxSide, dp[i][j]);
105+
}
106+
}
107+
}
108+
109+
// The area of the largest square is side length squared
110+
return maxSide * maxSide;
111+
}
112+
}
113+
114+
```
115+
116+
### Complexity Analysis
117+
118+
#### Time Complexity: O($m$ x $n$)
119+
120+
> **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.
121+
122+
#### Space Complexity: O($m$ × $n$)
123+
124+
> **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.
125+
126+
# References
127+
128+
- **LeetCode Problem:** [Maximal Square](https://leetcode.com/problems/maximal-square/description/)
129+
- **Solution Link:** [Maximal Square Solution on LeetCode](https://leetcode.com/problems/maximal-square/solutions/)
130+
- **Authors LeetCode Profile:** [Vivek Vardhan](https://leetcode.com/u/vivekvardhan43862/)

0 commit comments

Comments
 (0)