Я скачал leetcode Torture (расширение для гугла), после чего там далась эта задача, которую я не мог решить в течении часа, (потому что я не когда не заходил на leetcode) но вы обьяснили эту задачу так быстро и проффесионально. Большое спасибо! I downloaded the Leetcode Torture extension, and then I got this stupid problem that I was struggling with, like, for an hour, but you've solved it in just 20 minutes, and that quickly and professionally... with explained every step of the process. Thank you very much
I really like how you've arranged the videos in your playlist. The upcoming videos are somewhat related to the concepts learned in the previous lessons. Thanks!
Amazing explanation, got the understanding of firstRow and firstCol when I got stuck with the leetcode example where there is only a single 0 in middle of the matrix. Thanks.
Another solution without using extra space Start row wise first. Select rows one by one and make all the elements of that row -1 except which are 0, if any element in that row is 0. Similariy you have to do the same thing for columns. Now, before returning traverse the matrix and make all the -1 elements 0. public class Solution { public int[][] solve(int[][] A) { int n = A.length, m = A[0].length; for(int i = 0; i < n; i++){ int flag = 0; for(int j = 0; j < m; j++){ if(A[i][j]==0)flag = 1; } if(flag == 1){ for(int j = 0; j < m; j++){ if(A[i][j] != 0) A[i][j] = -1; } } } for(int j = 0; j < m; j++){ int flag = 0; for(int i = 0; i < n; i++){ if(A[i][j]==0)flag = 1; } if(flag == 1){ for(int i = 0; i < n; i++){ if(A[i][j] != 0) A[i][j] = -1; } } } for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ if(A[i][j] == -1)A[i][j] = 0; } } return A; } }
We only start from i=0 and j=0 for the first for loop, that is when you are setting the markers. After that if you observe the second for loop, we are starting from i=1 and j=1. If you go through the explanation again, we do this so we can use the first row and column to see if there is a zero anywhere in the respective row/column. Hope this helps.
if there is no row or column that has 0 in it and we updated 0 in it from another element then how can we use 1st row and column to make change in whole row and column
this gives incorrect Answer for this result matrix : [[1],[0],[3]] expected : [[0],[0],[0]] output: [[0],[0],[3]] --> this is in leet code , code which I wrote is below , let me know where I made mistake boolean firstRow = false, firstCol = false; // Set markers in first row and first column for(int i=0; i
try adding print statements in your code to debug, you will then be able to see the state of your matrix at certain stages. That should help to figure out
@@nikoo28 I have a doubt. It is for sure that we can't use the nested loop to iterate but while marking for zeros we have used a nested loop can u please solve my doubt.
@@fffooccc9801 you will have to iterate over each element in the matrix atleast once, and it cannot be done without a nested loop. It is the levels of nesting that you want to reduce…you should only have one nested loop..not more than that.
after doing so many leetcode questions like these, sometimes I feel like programming is all these hacks and tricks to make things more efficient. Anybody feels the same way?
that will not be possible, because in the worst case you will have to convert all elements to 0. which means going over each element, and that is O(m * n)
The solution is wrong. Also why did you convert all the remaining elements to zero in the last step? If you take the example from striver's video he particularly said about that element.
You explained the optimized part in the best way from all the videos on youtube, thanks!
finally understood the last method 😭 it took my day while learning from striver
Glad it helped!
literally, i was having a hard time learning the optimal solution from striver, that shit was like some rocket science to understand.
everyone has a different style of explaining. Sometimes striver might click better for you...other days it could be mine :)
Please continue with more array questions. Your explanation is top notch.
that is the only video where I could understand intuition
Я скачал leetcode Torture (расширение для гугла), после чего там далась эта задача, которую я не мог решить в течении часа, (потому что я не когда не заходил на leetcode) но вы обьяснили эту задачу так быстро и проффесионально. Большое спасибо!
I downloaded the Leetcode Torture extension, and then I got this stupid problem that I was struggling with, like, for an hour, but you've solved it in just 20 minutes, and that quickly and professionally... with explained every step of the process. Thank you very much
Да, рассмотрение проблемы с помощью визуальных эффектов очень помогает. Я так рад, что это было полезно для вас.
I really like how you've arranged the videos in your playlist. The upcoming videos are somewhat related to the concepts learned in the previous lessons. Thanks!
Glad you like them!
You are a great teacher. That was the best explanation I found so far. Keep teaching.
Thank you, I will
Amazing explanation, got the understanding of firstRow and firstCol when I got stuck with the leetcode example where there is only a single 0 in middle of the matrix.
Thanks.
Only you were able to explain it clearly for me, thanks brother
so happy to hear that.. :)
Wonderful explanation! Finally got it, sir
Another solution without using extra space
Start row wise first. Select rows one by one and make all the elements of that row -1 except which are 0, if any element in that row is 0. Similariy you have to do the same thing for columns.
Now, before returning traverse the matrix and make all the -1 elements 0.
public class Solution {
public int[][] solve(int[][] A) {
int n = A.length, m = A[0].length;
for(int i = 0; i < n; i++){
int flag = 0;
for(int j = 0; j < m; j++){
if(A[i][j]==0)flag = 1;
}
if(flag == 1){
for(int j = 0; j < m; j++){
if(A[i][j] != 0) A[i][j] = -1;
}
}
}
for(int j = 0; j < m; j++){
int flag = 0;
for(int i = 0; i < n; i++){
if(A[i][j]==0)flag = 1;
}
if(flag == 1){
for(int i = 0; i < n; i++){
if(A[i][j] != 0) A[i][j] = -1;
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(A[i][j] == -1)A[i][j] = 0;
}
}
return A;
}
}
marking them -1 doesnot increase time complexity ??
finally after watching many lecture your video is recommended and it's very helpful and clear my concept
please upload all dsa question , your explaination is best sir
The only part I was confused is the
for(int i = 1;i
We only start from i=0 and j=0 for the first for loop, that is when you are setting the markers. After that if you observe the second for loop, we are starting from i=1 and j=1.
If you go through the explanation again, we do this so we can use the first row and column to see if there is a zero anywhere in the respective row/column.
Hope this helps.
@@nikoo28 Thanks, understood
Very clear explanation!!. Thanks a lot.
if there is no row or column that has 0 in it and we updated 0 in it from another element then how can we use 1st row and column to make change in whole row and column
thanks!
Thanks Helpful!
this gives incorrect Answer for this result matrix : [[1],[0],[3]] expected : [[0],[0],[0]] output: [[0],[0],[3]] --> this is in leet code , code which I wrote is below , let me know where I made mistake
boolean firstRow = false, firstCol = false;
// Set markers in first row and first column
for(int i=0; i
try adding print statements in your code to debug, you will then be able to see the state of your matrix at certain stages. That should help to figure out
@@nikoo28 thanks for the reply, figured out the mistake, while checking for 1st column have been comparing I==0 , hence giving wrong answer .
kya baat hai sir maja aa gaya . loved this
we are with u sir thanks for make it so much easy soln
amazing explanation sir!!
Thank you sir... this is just an amazing approach
Very good explanation, please used to upload frequently, or if you have any community discord or other please tell us
I don’t have a discord yet…but can explore about it. Can you tell me its advantages and how it will be helpful to my viewers?
Please do upload videos regarding graph
algorithms your explanation style will make it easy to understand.
Sure…I am prepping up some material for it. Graphs are a little tricky…so need a little extra time to simplify them.
@@nikoo28 I have a doubt. It is for sure that we can't use the nested loop to iterate but while marking for zeros we have used a nested loop can u please solve my doubt.
@@fffooccc9801 you will have to iterate over each element in the matrix atleast once, and it cannot be done without a nested loop.
It is the levels of nesting that you want to reduce…you should only have one nested loop..not more than that.
The complete playlist on graphs is now available: th-cam.com/play/PLFdAYMIVJQHNFJQt2eWA9Sx3R5eF32WPn.html
your explanation is too good keep rocking
Thank you so much 🙂
great explanation!!
Glad you liked it!
Thanks
Sir, Why we are using Matrix[0].length & Matrix.length in some for loops?
What is the difference between them
Great sir ❤
Thanks understood
after doing so many leetcode questions like these, sometimes I feel like programming is all these hacks and tricks to make things more efficient. Anybody feels the same way?
sorry have a question, why is this a constant space solution where there are nested loops multiple times? thanks so much
just using loops does not mean extra space. You are said to be taking extra space when you are using extra memory equivalent to your input size.
Great !!!!!
Teaching mast hai bhai aapki
Let me what other type of questions/topics you want to see.
@@nikoo28 Please make on Arraylist and linkedlist questions
And
Graphs, dynamic programming, hashset , hashmap
@@nikoo28 i will send you the questions one by one
The complete playlist on graphs is now available: th-cam.com/play/PLFdAYMIVJQHNFJQt2eWA9Sx3R5eF32WPn.html
wow i really dont like matrix problems. the best solution uses 2 nested for loops and 2 more for loops
Yes, matrix problems are very wonky.
even in the second and third solution Time complexity is M*N , cant we reduce the time complexity to M+N ??
that will not be possible, because in the worst case you will have to convert all elements to 0. which means going over each element, and that is O(m * n)
The solution is wrong. Also why did you convert all the remaining elements to zero in the last step? If you take the example from striver's video he particularly said about that element.
what do you mean when you say the solution is wrong? The solution passes all the given test cases on LeetCode.
but sir's video is earlier than his 😅😅
Thanks