You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dsa-solutions/lc-solutions/0200-0299/221 - Maximal Square.md
+5-5Lines changed: 5 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
id: Maximal Square
2
+
id: maximal-square
3
3
title: Maximal Square
4
4
sidebar_label: 221 Maximal Square
5
5
tags:
@@ -12,7 +12,7 @@ description: "This document provides a solution where we find the largest square
12
12
13
13
## Problem
14
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.
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
16
17
17
### Examples
18
18
@@ -43,7 +43,7 @@ You are given an m x n binary $matrix$ filled with $0's$ and $1's$, find the lar
43
43
- $m == matrix.length$
44
44
- $n == matrix[i].length$
45
45
- $1 <= m, n <= 300$
46
-
- $matrix[i][j]$ $is$ $'0'$ $or$ $'1'$
46
+
- $matrix[i][j]$ is $0$ or $1$
47
47
48
48
---
49
49
## Approach
@@ -56,7 +56,7 @@ There are four approaches discussed that helps to obtain the solution:
56
56
57
57
2.**Transition**:
58
58
59
-
- If **'matrix[i][j]'** is $'1'$:
59
+
- If **'matrix[i][j]'** is $1$:
60
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
61
62
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$.
@@ -119,7 +119,7 @@ class Solution {
119
119
120
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
121
122
-
#### Space Complexity: O($m$ × $n2$)
122
+
#### Space Complexity: O($m$ × $n$)
123
123
124
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.
0 commit comments