For Those who are thinking why tc passes when he pass vector by refernce : Passing by value keeps the original vector unchanged and doesn’t modify the original values of the vector. However, the above style of passing might also take a lot of time in cases of large vectors. So, it is a good idea to pass by reference.
O(1) space complexity is based on just making the given matrix as a dp memory. int space_optimization1(int m ,int n, vector&mat){ int maxi = 0; for(int i=n-1; i>=0; i--){ for(int j=m-1; j>=0; j--){ int down = i
int maxSquare(int n, int m, vector mat){ // code here int ans=0; for(int i=n-2;i>=0;i--) { for(int j=m-2;j>=0;j--) { if(mat[i][j]==1) mat[i][j]=1+min(mat[i+1][j],min(mat[i+1][j+1],mat[i][j+1])); ans=max(ans,mat[i][j]); } } for(int j=0;j
we can use contraints in problem statment it says m can be at max 50. so, creating a vector of size 50 hence O(1) space complexity. Correct me if I am wrong
bhyia...ek baar aap dobara se ek DSA roadmap ki video bna doge..jisme aap ye v mention krna...aakhir kaar DSA me kon kon se TOPICS aate hai or KON KON S ALGO. please bhyiaaa
Regarding the hw, we can't use a Data structure but variables; there are total four variables we need; for curr[j], curr[j+1], next[j] and next[j+1]..And after every iteration, var(curr[j])=var( next[j] ) and var(curr[j+1])=var( next[j+1])..right?
we can convert our O(m) space complexity solution in O(1) space complexity using the code given bellow : // we can use mat vector as our dp vector. int solveSpaceOptimized2(vector mat){ int row = mat.size(); int col = mat[0].size(); int maxi = 0; for(int i=row-1;i>=0;i--){ for(int j=col-1;j>=0;j--){ if(i == row - 1 || j == col - 1){ maxi = max(maxi , mat[i][j]); } else{ if(mat[i][j] == 1){ int right = mat[i][j+1]; int down = mat[i+1][j]; int diagonal = mat[i+1][j+1]; mat[i][j] = 1+min(right , min(down , diagonal)); maxi = max(maxi , mat[i][j]); } } } } return maxi; }
class Solution{ public: int maxSquare(int n, int m, vector mat){ int ans = 0; for(int i = 0;i < n;i++){ for(int j = 0;j < m;j++){ if(!i || !j) ans = max(ans,mat[i][j]); else if(mat[i][j]){ mat[i][j] += min(min(mat[i-1][j-1],mat[i-1][j]),mat[i][j-1]); ans = max(ans,mat[i][j]); } } } return ans; } };
@@vedantraut256 As a thumb rule , just remember size of dp vector used is the complexity. Here size of dp is m x n , and hence the complexity would be O(m*n) Logic behind this thumb rule is , you calculate each dp state only once.
Hi Love sir, I am a beginner to DSA. Started with this series and reached lecture 15. Jo questions ap video me karwate ho that i do on leetcode and codestudio. But LC and CS me 1000s questions aur bhi h, so samajh nhi ata ki konsa question karu and konsa important nhi h. Kya ap guide kar sakte ho iske baare me?
solvetab has return type int but while calling it we dont use any variable to store the value returned by it. still the code doesnt show any error can anyone pls explain?
cause the rerturn type of function is int type ,,, toh kuch integer toh return krne padega ... u can change it to void type ,, cause ANSWER toh maxi me store hora h
iski need space optimization me h, otherwise rahne do. because so wale me apan ko curr array me changes karne padenge for each value, but on others, you can skip it, maine b ni use kiya tha, just error face kiya 😂😁😁
I was writing this code in java (only recursion) but somehow this throws error, can somebody point it out pls class Solution { static int maxSquare(int n,int m,int mat[][]) { int max=0; recursion(mat,0,0,max); return max; } static int recursion(int[][] matrix,int i,int j,int max) { if(i=matrix[0].length || matrix[i][j]==0) return 0; int right = recursion(matrix,i,j+1,max); int diag = recursion(matrix,i+1,j+1,max); int down = recursion(matrix,i+1,j,max); if(matrix[i][j]==1) { int ans = 1 + Math.min(right,Math.min(diag,down)); max = Math.max(max,ans); return ans; } else { return 0; } } }
Bro, you are returning something from a function, but not receiving it at the calling point, hence error in java(strict language but this is a good thing) but this in c++(somewhere between java n python) is running the code
For Those who are thinking why tc passes when he pass vector by refernce :
Passing by value keeps the original vector unchanged and doesn’t modify the original values of the vector. However, the above style of passing might also take a lot of time in cases of large vectors. So, it is a good idea to pass by reference.
O(1) space complexity is based on just making the given matrix as a dp memory.
int space_optimization1(int m ,int n, vector&mat){
int maxi = 0;
for(int i=n-1; i>=0; i--){
for(int j=m-1; j>=0; j--){
int down = i
It varies from constraints to constraints.
@@aamirwani7413 obviously but u cant optimize further ....if u can then reply here👍
int maxSquare(int n, int m, vector mat){
// code here
int ans=0;
for(int i=n-2;i>=0;i--)
{
for(int j=m-2;j>=0;j--)
{
if(mat[i][j]==1)
mat[i][j]=1+min(mat[i+1][j],min(mat[i+1][j+1],mat[i][j+1]));
ans=max(ans,mat[i][j]);
}
}
for(int j=0;j
we can use contraints in problem statment it says m can be at max 50. so, creating a vector of size 50 hence O(1) space complexity.
Correct me if I am wrong
@@vedantraut256 can it be solved using 4 variables?
Best series on DP🧡🧡
Hats of to you sir you are doing a great job and helping lot of students to grab placement. Thankyou soo much sir god bless you.
🙃 you are the best sir
Bhaiya why are DBMS videos not coming?? 😕
How do we know that when using tabulation method, whether we should initialize the vector with 0, or INT_MAX, or INT MIN
Loving the series ❤️❤️❤️❤️❤️❤️❤️❤️
Honestly speaking, H/W nhi bn rha h..... but one thing... dil jeet lia aapne phir se.. and vo space optimization wla was lit!
bhyia...ek baar aap dobara se ek DSA roadmap ki video bna doge..jisme aap ye v mention krna...aakhir kaar DSA me kon kon se TOPICS aate hai or KON KON S ALGO. please bhyiaaa
bhaiya dbms series kyuband hai ?
thank you bhaiya
maza aa gaya bhaiya
Bhaiya DBMS series ke next video kab aayege? Jaldi se complete kar do placement aane wale hai plz bhaiya 😭
why does he return ans and not maxi
Bhai DBMS course ka kuch karo please.
11:56 updated maxi but still returning ans
Good work
Regarding the hw,
we can't use a Data structure but variables;
there are total four variables we need; for curr[j], curr[j+1], next[j] and next[j+1]..And after every iteration, var(curr[j])=var( next[j] ) and var(curr[j+1])=var( next[j+1])..right?
No
Bhaiya bahut wait Kiya aape videos ke liye 🥲kitne dino ke baat 2 videos aae apki 🥲🥲
Hi
Love you ❤️❤️❤️❤️❤️❤️ bhaiya ji
HW solution: Do the changes in given matrix itself instead of creating 2D dp
bhai if i feel comfortable in top down approach ussi mein kar sakte hain sare problem
you are great sir
we can convert our O(m) space complexity solution in O(1) space complexity using the code given bellow :
// we can use mat vector as our dp vector.
int solveSpaceOptimized2(vector mat){
int row = mat.size();
int col = mat[0].size();
int maxi = 0;
for(int i=row-1;i>=0;i--){
for(int j=col-1;j>=0;j--){
if(i == row - 1 || j == col - 1){
maxi = max(maxi , mat[i][j]);
}
else{
if(mat[i][j] == 1){
int right = mat[i][j+1];
int down = mat[i+1][j];
int diagonal = mat[i+1][j+1];
mat[i][j] = 1+min(right , min(down , diagonal));
maxi = max(maxi , mat[i][j]);
}
}
}
}
return maxi;
}
good bro
hello sir , could you please tell me why we are tracing the matrix in backward direction in solve in tabular form
can solve it the either way, just need to change few lines inside the loop
Thanks sir
bhaiyaa aapko pata kese chalta hai ki kis function me kya kya pass krna hota hai ?
class Solution{
public:
int maxSquare(int n, int m, vector mat){
int ans = 0;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(!i || !j) ans = max(ans,mat[i][j]);
else if(mat[i][j]){
mat[i][j] += min(min(mat[i-1][j-1],mat[i-1][j]),mat[i][j-1]);
ans = max(ans,mat[i][j]);
}
}
}
return ans;
}
};
bhai iska logic bta skte ho kya
Great going!!!
Approach 3:40
Compile @11:20
bhaiya aapne strings mein pattern searching algos nahi karaye. Uski bhi ek video daal sakte h
Wo,naive, KMP n rabin kurp wagera
How long dsa course will take.plz complete it as fast as you can🙏🙏🙏🙏
no sir take your time. no hurry.
Bro usne pehle hi selected Q bna rkhe h dp k vo krlo usne khud bola tha jisko jaldi chalna h
5/10 consistency++
aaur mujhe party denge was personal :)
Bhaiya function ke return type int hai and aap main se sirf call kar rahe ho , chal kaise raha hai ? Please help to understand this..
It's just we are not storing the return value in any variable, that is not creating any container
I couldn't code the recursive one in java. Someone pls share it.
Why it gave TLE when mat was not sent by reference?
it will create multiple copies of matrix which is causing the problem, if u pass it by reference it will just point to the original
Sir , i am here to tell u im am following ur series , but rn my exams are going on so couldnt watch this video
Can Space Complexity be O(1) if we use curr and next vector of size 50?
I am saying this as in GFG the constraints for n and m are :- 1
complexity is when your space use depends on size of input. So it's technically not O(1)
@@ADITYAKUMAR-tb4gm then how much will it be?
@@vedantraut256 As a thumb rule , just remember size of dp vector used is the complexity. Here size of dp is m x n , and hence the complexity would be O(m*n)
Logic behind this thumb rule is , you calculate each dp state only once.
@@ADITYAKUMAR-tb4gm But I am creating a vector of size 50 then?
@@vedantraut256 That's because you know the constraints. If you use 50 x 50 space without knowing actual constraints , then it would be O(1)
Hi Love sir, I am a beginner to DSA. Started with this series and reached lecture 15. Jo questions ap video me karwate ho that i do on leetcode and codestudio. But LC and CS me 1000s questions aur bhi h, so samajh nhi ata ki konsa question karu and konsa important nhi h. Kya ap guide kar sakte ho iske baare me?
Sir ne 450 question ki list banayi huvi hai 1 year ago dekh lo
Great explaination
crazy
waaaahhhh
This method is not working for my java code :)
Approach-1 is wrong if you think matrix[i][j]=0 but actual might be >=0
11:40
14:40
20:44
pls provide source code from lecture 65 onwards
what is the time complexity of recursive approach?? Can anyone expalin?
i think O(3N^2)
Recurrence relation would be
T(n,m)= T(n-1,m) + T(n-1,m-1) + T(n,m-1) + constant(c)
20:48 tabulation another method
wah
u should also provide java code ?
solvetab has return type int but while calling it we dont use any variable to store the value returned by it. still the code doesnt show any error can anyone pls explain?
It's the maxi we want.
Not the "ans"
Can anyone tell me why he returned dp[0][0] in tabulation.
cause the rerturn type of function is int type ,,, toh kuch integer toh return krne padega ... u can change it to void type ,, cause ANSWER toh maxi me store hora h
Dhynabwad++
resume from 22:05
ye else { curr[j] = 0 } ki jaroorat kyu hai bhai?
iski need space optimization me h, otherwise rahne do.
because so wale me apan ko curr array me changes karne padenge for each value, but on others, you can skip it, maine b ni use kiya tha, just error face kiya 😂😁😁
🔥🔥
SC O(1) me kar to liya lekin pura code to o(n*m) hi lega kiyoki main function me to 2D vector hai
Code diyo bhai mera chal ni ra kuchh testcases mein
@@guneetsingh4059
int maxSquare(int n, int m, vector mat){
//vectordp(n+1,vector(m+1,0));
int maxi=0;
vectorcur(m+1,0);
vectornext(m+1,0);
for(int i=n-1;i>=0;i--){
for(int j=m-1;j>=0;j--){
int r= ((i+1)
@@CricketGalaxy07-18 Thanks bhai
@@CricketGalaxy07-18 Nice work bro!
@@CricketGalaxy07-18 Why did it work for mat[i][j] and not for cur[j] inside the if statement? Help me out.
Bhaiaya agar ho sakeytoh java code b dedo
Is it possible to enable subtitles option?
// COMPLETE CODE WITH COMMENTS AND HOMEWORK
class Solution{
public:
int solve(int i, int j, vector &mat, int &maxi){
if(i >= mat.size() || j >= mat[0].size()){
return 0;
}
int right = solve(i, j+1, mat, maxi);
int diagonal = solve(i+1, j+1, mat, maxi);
int down = solve(i+1, j, mat, maxi);
if(mat[i][j] == 1){
int ans = 1 + min(right, min(diagonal, down));
maxi = max(maxi, ans);
return ans;
}
else
return 0;
}
int solveMem(int i, int j, vector &mat, int &maxi, vector &dp){
if(i >= mat.size() || j >= mat[0].size()){
return 0;
}
if(dp[i][j] != -1)
return dp[i][j];
int right = solveMem(i, j+1, mat, maxi, dp);
int diagonal = solveMem(i+1, j+1, mat, maxi, dp);
int down = solveMem(i+1, j, mat, maxi, dp);
if(mat[i][j] == 1){
dp[i][j] = 1 + min(right, min(diagonal, down));
maxi = max(maxi, dp[i][j]);
return dp[i][j];
}
else
return dp[i][j] = 0;
}
int solveTab(vector &mat, int &maxi){
int row = mat.size();
int col = mat[0].size();
vector dp(row+1, vector (col+1, 0));
for(int i = row-1; i>=0; i--){
for(int j = col-1; j>=0; j--){
int right = dp[i][j+1];
int diagonal = dp[i+1][j+1];
int down = dp[i+1][j];
if(mat[i][j] == 1){
dp[i][j] = 1 + min(right, min(diagonal, down));
maxi = max(maxi, dp[i][j]);
}
else
dp[i][j] = 0;
}
}
return 0;
}
int solveSpaceOP1(vector &mat, int &maxi){
int row = mat.size();
int col = mat[0].size();
vector curr(col, 0);
vector next(col+1, 0);
for(int i = row-1; i>=0; i--){
for(int j = col-1; j>=0; j--){
int right = curr[j+1];
int diagonal = next[j+1];
int down = next[j];
if(mat[i][j] == 1){
curr[j] = 1 + min(right, min(diagonal, down));
maxi = max(maxi, curr[j]);
}
else
curr[j] = 0;
}
next = curr;
}
return 0;
}
// HOMEWORK
// Here Space Complexity is O(1) as n and m at max can be 50
// By referring to the constraints in problem statment :- 1=0; j--){
int right = curr[j+1];
int diagonal = next[j+1];
int down = next[j];
if(mat[i][j] == 1){
curr[j] = 1 + min(right, min(diagonal, down));
maxi = max(maxi, curr[j]);
}
else
curr[j] = 0;
}
next = curr;
}
return 0;
}
int maxSquare(int n, int m, vector mat){
// RECURSION
/*
int maxi = 0;
solve(0, 0, mat, maxi);
return maxi;
*/
// RECURSION + MEMOIZATION
/*
vector dp(n, vector (m, -1));
int maxi = 0;
solveMem(0, 0, mat, maxi, dp);
return maxi;
*/
// TABULATION
/*
int maxi = 0;
solveTab(mat, maxi);
return maxi;
*/
// SPACE OPTIMIZATION - 1
/*
int maxi = 0;
solveSpaceOP(mat, maxi);
return maxi;
*/
// SPACE OPTIMIZATION - 2 --> HW Que --> S.C. = O(1)
int maxi = 0;
solveSpaceOP2(mat, maxi);
return maxi;
}
};
I was writing this code in java (only recursion) but somehow this throws error, can somebody point it out pls
class Solution {
static int maxSquare(int n,int m,int mat[][]) {
int max=0;
recursion(mat,0,0,max);
return max;
}
static int recursion(int[][] matrix,int i,int j,int max)
{
if(i=matrix[0].length || matrix[i][j]==0)
return 0;
int right = recursion(matrix,i,j+1,max);
int diag = recursion(matrix,i+1,j+1,max);
int down = recursion(matrix,i+1,j,max);
if(matrix[i][j]==1)
{
int ans = 1 + Math.min(right,Math.min(diag,down));
max = Math.max(max,ans);
return ans;
}
else
{
return 0;
}
}
}
Try removing the matrix[i][j]==0 condition; rn the recursion would stop if any cell with 0 is encountered...
Bro, you are returning something from a function, but not receiving it at the calling point, hence error in java(strict language but this is a good thing) but this in c++(somewhere between java n python) is running the code
Web dev videos are not up to time bhaiya 😢
15:33 what changed there? could anybody explain the reason why the change works?
The dp vector was not passed by reference, if u are still confused you can search about pass by reference and pass by value
@@shobhitsaha8901 Thanks for your reply sorry but my question was how passing by reference resolved TLE.
90 ✅completed 👍Liked 03:07
int solveSpaceOpti2( vector< vector > &mat, int &n, int &m)
{
//by columns
int maxi = 0;
vector curr(n+1,0);
vector next(n+1,0);
for(int j = m-1 ; j>=0 ; j--) {
for(int i = n-1; i>=0 ; i--) {
int down = curr[i+1];
int right = next[i];
int diag = next[i+1];
if( mat[i][j] == 1 )
{
int ans = 1 + min(down,min(right,diag));
maxi = max(ans,maxi);
curr[i] = ans;
// return ans;
}
else {
curr[i] = 0;
}
}
next = curr;
}
return maxi;
}
swad++
Atleast explain why you are taking minimum in the transitions. Disappointing.
bro he explained , dont skip the video while watching
a
O(1) complexity solution
void spaceOP( vector &mat,int n,int m,int &maxi){
for(int i=n-1;i>=0;i--){
for(int j=m-1;j>=0;j--){
int right= (j+1>=m)?0:mat[i][j+1];
int down= (i+1>=n)?0:mat[i+1][j];
int dag=(i+1>=n || j+1>=m )?0:mat[i+1][j+1];
if(mat[i][j]==1){
mat[i][j]=1+min(right,min(down,dag));
maxi=max(maxi,mat[i][j]);
}
}
}
}
int maxSquare(int n, int m, vector mat){
// code here
//vectordp(n,vector(m,-1));
int maxi=0;
spaceOP(mat,n,m,maxi);
return maxi;
}
e
t
abe yrr jab nhi samjha paa rha hai 20 minute me toh time badha lekin acche se smjha na..
If you can't understand what he is teaching then you should left learning to code .