Maximal square | Dynamic programming | Leetcode #221 | Largest Square Submatrix of all 1's

preview_player
Показать описание
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:

n == matrix[i].length
matrix[i][j] is '0' or '1'.
Рекомендации по теме
Комментарии
Автор

i thought this was very complexed problem. after watching this video, i got the confidence to solve this problem. Thanks alisha!

thellaidhinesh
Автор

thank you! this explanation is far more clear than those i watched before

williamaldridge
Автор

I got the answer after dry run with the example. BTW thanks a lot 😍😍😍 for the great explanation. But I am thinking that we can do this problem using top down approach as well (memoization).

pankajyadavquantum
Автор

The intuition behind this algo is that we are checking for all possible squares ending at the given index.
So, we are using the results of maximum sized squares ending at 3 adjacent indexes and using that information to find out the max possible solution at current index.

ankk
Автор

please add the link as well and ur playlists are best way to revise and the way you explain gives me confidence to speak in interviews ..
Thanks a lot for amazing content

shivanshpareek
Автор

Space Complexity: O(1)

int maxSquare(int n, int m, vector<vector<int>> mat){
// code here
int ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
ans=max(ans, mat[i][j]);
}
if(ans==1)
{
break;
}
}
for(int i=1;i<n;i++)
{
for(int j=1;j<m;j++)
{ if(mat[i][j]==1){
int x=min(min(mat[i-1][j], mat[i][j-1]), mat[i-1][j-1]);
ans=max(ans, x+1);
mat[i][j]=x+1;
}
}
}
return ans;
}

BereniceVirginis
Автор

Ufff didi one of the best place and you just explained the code in really easy way, I saw everyone's video but they just copy pasting the solution ❤❤😊

shivanshyadu
Автор

I have a dought, you are returning the max area by 3* 3 but the right area is 2*3 right???

PAVITHRAR-xc
Автор

The way you are conveying the intuition part of the solution creates the difference.
Nice choice of questions btw.

prasannaprabhakar
Автор

Thank you so much for this approach ... this is really easy

Coding_Destini
Автор

I mam I want talk with your about my features opportunity where can connect with you

govinth
Автор

inside if( i = = 0 || j = = 0) you are doing unnecessary work you can leave it as it is, nice explanation overall 🙏thank you

tiger
Автор

Can we solve problems by filling dp from last cell to first

duck
Автор

Great explanation! This helped a bunch!

kpjVideo
welcome to shbcf.ru