its not a big deal though, but it really ensures that u r growing as a coder ,keep it up bruh.regarding second approach i though that instead of using two arrays row and column,we can just use one array of pair type where each element of the array is a pair whose first element is ,arked row and second elemen is marked column.
Understood! Very well 🔥🔥🔥. And half a hour to example every step from intuition ---> approach till code it shows how you work hard to provide such wonderful video's thanks a lot for this efforts brother.
Let's imagine you're in charge of a high-tech building with a security system that monitors various rooms. The building's layout can be visualized as a grid (matrix), where each cell in the grid represents a room. The value in each cell can either be: 1: The room is secure and everything is functioning normally. 0: The room has a security breach (e.g., a broken window, unauthorized entry, etc.). The building's protocol is to lock down (disable) any room and its neighboring rooms (in the same row and column) immediately if a breach is detected. This ensures that the breach can't spread to other areas.
Code from older video is better. Also I understood the intuition in this video's explanation by understanding it from older video. A request - PLEASE DON'T DELETE OLDER VIDEOS EVER, THEY TOO ARE GEM ❤
#Free Education For All.. # Bhishma Pitamah of DSA...You could have earned in lacs by putting it as paid couses on udamey or any other elaerning portals, but you decided to make it free...it requires a greate sacrifice and a feeling of giving back to community, there might be very few peope in world who does this...."विद्या का दान ही सर्वोत्तम दान होता है" Hats Off to you man, Salute from 10+ yrs exp guy from BLR, India.....
Bhaiya abhi shayad itne views nhi aarhe but rojj video daalte raho iss course ko complete karna and this will smash all education viewing records. Salute++
thankjs a lot bro I've watched lots of paid videoes and after that i came here to this master piece playlist I have no words how much it helps me to crack a job guys do not buy any DSA courses just watch all the videoes it will be enough to crack any DSA interview🤗🥰🥰🥰🥰🥰🥰
Really amazing Striver. In-place space optimisation method is really out of my imagination. you make everyting real in very much optimal way. Thankyou Striver for such an amazing tutorial ❤
Recently started learning DSA, i was able to come up with the same approach as 1st and 2nd, even though I couldn't complete it but I'm happy I came with some approaches. Thanks for this great explanation.
Thanks striver, ....however i would like to mention my code....when i solved the code by myself....i used a treemap to keep an account of the zeroes.....and then iterated through the map turning all the stored rows and columns to 0....however actually this approach came to my mind from your vertical order traversal of binary tree where you used almost the same data structure....and my solution was accepted (though the T.C was bad)....but still thanks for helping to build me my own approach....and the optimal solution was superb...
Wonderful approach. Thank you soo much for putting so much efforts and helping us learn these algorithms. No words are enough to express how much your efforts mean to us🙏
OMG , I had watched many tutorials for this problem but never understood it clearly . Thankyou very much for your step-by-step detailed explanation , looking for more content from you in future🙂
After following your videos, I'm able to understand the problems on my own and even come up with optimal solutions myself. Thanks for the amazing videos.
You gave me hope when it comes to dsa... I'm too late being misled by other videos, but now after discovering your sde sheet, I have started to figure everything out in a planned manner... soooo... thank youuuuuuuuuuuu!!!!
Hi Striver, I the last approach can also be written as below which looks less complex and easy to understand int n = matrix.size(); int m = matrix[0].size(); int col0 = 1; for(int i=0; i=1; j--){ if(matrix[0][j] == 0 || matrix[i][0]==0){ matrix[i][j] = 0; } } if(col0 == 0){ matrix[i][0] = 0; } }
I had not understood when I watched it 2-3 days back. Looks like I had watched a video which was recorded 2 years back lol. But this new video is crisp clear
Was able to come up with better solution but did little mistake in the implementation , happy to see that I am now able to come with logical approach to solve problems All thanks to striver bhaiya! ❤
Amazing explanation, thank you! For the second pass through the array, I tried a slighly shorter approach which seems to work as well (tested on Leetcode). Process the elements bottom to top, right to left. This ensures that an element (i,j) is always updated before its row indicator (matrix[i][0]) and col indicator (col0 if j==0 else matrix[0][j]) are processed. We can then update them through single pair of loops over the array and we may not need special handling for matrix[0][0] and col0 elements.
im going with your sheet, but this question is of 2D arrays which is a bit challenge for me as i didt did any of 2 D questions yet should have given a intro before 2D questions
for optimal approach, while filling the matix, instead we can directly traverse the matrix In reverse order so that matrix[0][0] will be affected at last. i mean run loop for(row=m-1 to 0) { for(col=n-1 to 0)}. it worked 😀😀 //fill the matrix for(int i=m-1;i>=0;i--){ for(int j=n-1;j>=0;j--){ if(j==0){ if(col0==0)matrix[i][j]=0; } else{ if(matrix[i][0] == 0 || matrix[0][j] == 0) matrix[i][j]=0; } } }
Bhai kya explanation hai ekdam next level. actually bhai videos mai aap c++ ka code lekhtai ho and Mai aur mare hostel friends ham Java mai code karte hain isleye thoda uncomfortable lagta hai . Aap ke article me jata hu to purana code milta hai . please Bro add Java code in to videos it will be beneficial to lot of students otherwise this course will be end and we are the java people will unable to to follow.Please bro🙏🙏🙏 .
The bruteforce approach code provided would fail for the testcase [[-1],[2],[3]] on Leetcode. Here is modified code for BruteForce class Solution { public void setZeroes(int[][] matrix) { int m = matrix.length; int n = matrix[0].length; int[] rows = new int[m]; int[] cols = new int[n]; // Mark rows and columns that contain zeros for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (matrix[i][j] == 0) { rows[i] = 1; // Mark the row cols[j] = 1; // Mark the column } } } // Set zeros based on the marked rows and columns for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (rows[i] == 1 || cols[j] == 1) { matrix[i][j] = 0; } } } } }
STRONG SUGGESTION for ease of understanding: In the better/optimal solution instead of doing the second iteration over the entire matrix just iterate over the rows and cols marked for 0-conversion (except the first row/col) and convert the elements to 0. In optimal just keep two separate variables like row0 and col0 and convert the first row/col at the very end (after all the other row/cols) are processed.
What if we do this? - use first row of matrix from 1 to m to mark columns use first column of matrix from 1 to n to mark rows keep two flags to check is first column or first row have a zero for(i=1;i set first row 0
Bhaiya like in brute force approach we are converting elements to -1 suppose -1 is already present in the arrray then it will modify the matrix and that we won't want.
Because since it is an integer matrix What if the matrix elements in the beginning is -1 then how can you use this approach of marking with -1 think about it .
Nice explanation of all the approaches however one feedback I have is at 14:35 video time there is some correction instead of saying minimizing space complexity you said minimizing time complexity. Thank You !!!
There is an inherent fallacy in the suggested brute force approach. The fact that the matrix values can be -1 by default is ignored. The cell value range b/w -2^31 to 2^31-1 as per the linked question. So we can get unnecessary zeroes.
GODDDDDDDDDDDDDDDDDDDDDD, I FINALLYYYYYYYYYYYYYY UNDERSTODDDDDDDDDDDDDDD THISSSSSSSSSSSSSSS!!!!!!!!!!!!! YAYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY edit: run hi nahi hua
it's simple buddy , just read below written words and you will get all the concepts cleared 1. Why optimal solution works ? isn't it wrong to change values of first row and column to zeroes , what happens to their own values ? --------- bro , it works because the values of first row and column that are changed to zeroes to be used as marking for later, actually happens to be the values that they finally would have got , so those marking prove to be the actual final values that they are getting , ( this I got when i compared better approach and optimized approach side by side and saw that there is no difference even if 1st row and column acts as marking arrays for respective columns and rows. ) 2. Why do we traverse the way we are doing , that is first update every values except , of 1st row and column -------- because , 1st row and column has to be used as marking , and they must not change before their role as a marking isn't been completed. 3. Why we first update 1 row , then 1 column -------- because again , if 1st column is updated first , then we don't always get right marking for the 1st row , so 1 row needs to be updated first and then 1st column.