Set Matrix Zeroes | O(1) Space Approach | Brute - Better - Optimal

Поделиться
HTML-код
  • Опубликовано: 25 авг 2024

Комментарии • 478

  • @takeUforward
    @takeUforward  11 месяцев назад +27

    Please watch our new video on the same topic: ruclips.net/video/N0MgLvceX7M/видео.html

  • @saisree04
    @saisree04 3 месяца назад +98

    Mind got blown away after listening to the optimal solution

  • @takeUforward
    @takeUforward  Год назад +117

    Let's march ahead, and create an unmatchable DSA course! ❤
    Use the problem links in the description.

  • @aaryakapoor8352
    @aaryakapoor8352 Месяц назад +18

    mind got blown with the optimal approach

  • @CodeBoost8375
    @CodeBoost8375 Год назад +90

    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.

  • @suyashpurwar631
    @suyashpurwar631 Год назад +113

    I was able to do the second approach by myself and I'm feeling really good about it!

    • @RohanKumar-gy4gl
      @RohanKumar-gy4gl 11 месяцев назад +48

      HOLY S... bruh is an einstein for sure

    • @adityamadhav1509
      @adityamadhav1509 Месяц назад

      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.

    • @Carson00_11
      @Carson00_11 Месяц назад +1

      i am the only dumb then because i cant do anything

    • @jacobblack2544
      @jacobblack2544 Месяц назад

      @@Carson00_11 yes XD

    • @AkshanshLohan
      @AkshanshLohan 22 дня назад +5

      @@Carson00_11never say something negative about yourself even as a joke

  • @dpxy1599
    @dpxy1599 10 месяцев назад +108

    This optimal approach is confusing.

  • @jatinsharma1595
    @jatinsharma1595 8 месяцев назад +14

    Understood. Happy that I came up with the better approach by myself, seriously blown away by the optimal approach. Thank you so much Striver

  • @saisree04
    @saisree04 3 месяца назад +19

    mind got blown away after listening to the optimal solution

  • @josephwilliams9
    @josephwilliams9 10 месяцев назад +30

    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 ❤

  • @srinijabhogoju9033
    @srinijabhogoju9033 День назад +1

    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🙏

  • @shubhamagarwal1434
    @shubhamagarwal1434 21 день назад +2

    #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.....

  • @vivekdalal1724
    @vivekdalal1724 Год назад +15

    this is the only study related playlist which im binging by choice and loving it

  • @Manishgupta200
    @Manishgupta200 Год назад +13

    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 ❤

  • @AbhishekSingh-mh2fg
    @AbhishekSingh-mh2fg 11 месяцев назад +8

    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.

  • @devanshulpratapsingh5963
    @devanshulpratapsingh5963 Год назад +7

    So desperately trying this question for 2 days wasn’t able to optimise it, Now I can❤🔥

  • @krishsachdeva1105
    @krishsachdeva1105 Год назад +4

    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++

  • @Akash-yr2if
    @Akash-yr2if Год назад +3

    Gained some Brain Cells After doing the Optimized, Still getting in my head, Damn Solution 🤯🤯

  • @yugal8627
    @yugal8627 Год назад +4

    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🙂

  • @dxan
    @dxan 5 месяцев назад +2

    Your teaching skills are very well and I must say they are on another level. Thank you so much for the content.

  • @sanjayv-ef6ey
    @sanjayv-ef6ey Месяц назад +2

    I have a seen a ad full for u bhai thanks

  • @krishanubiswas6869
    @krishanubiswas6869 8 месяцев назад +3

    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...

  • @sukritinarang5016
    @sukritinarang5016 6 месяцев назад +4

    The brute force solution that set values to -1 instead of 0 will not work in some test cases as constraints are mentioned as -2^31

  • @shloksuman8164
    @shloksuman8164 Год назад +8

    Optimal approach was quite tedious to understand, however , I got it after writing down the code again myself

    • @takeUforward
      @takeUforward  Год назад +20

      Yes that is the reason I went slow and long, but writing it yourself helps to clear so many things.

  • @sakthipriya8653
    @sakthipriya8653 9 месяцев назад +5

    Understood sir, very great explanation. I was very excited in the 7th minute itself. 🔥🔥🔥

  • @unknown_fact1586
    @unknown_fact1586 22 дня назад

    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.

  • @yashwairagade3677
    @yashwairagade3677 5 месяцев назад +2

    U r a legend bro i am impressed by your problem solving skills

  • @karanhaldar755
    @karanhaldar755 5 месяцев назад +2

    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🤗🥰🥰🥰🥰🥰🥰

  • @nivetha6167
    @nivetha6167 Год назад +24

    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!!!!

    • @bully9858
      @bully9858 11 месяцев назад +4

      u are literally me

    • @nivetha6167
      @nivetha6167 11 месяцев назад

      @@bully9858 all the best with your coding Journey :⁠-⁠)

    • @GOJOANDSUKUNAFAN
      @GOJOANDSUKUNAFAN 2 месяца назад

      ​@@bully9858bro is actually him🗣️ 🔥🔥

  • @hitmanop4078
    @hitmanop4078 3 месяца назад +2

    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

  • @hashcodez757
    @hashcodez757 3 месяца назад +1

    great brother!!
    just can't imagine the optimizations you made
    hats off sirr!!

  • @Atharva_kathiyawadi
    @Atharva_kathiyawadi Год назад +3

    along with i really learn how to write clean code & improve readability !! thanks striver

  • @hari7110
    @hari7110 6 месяцев назад +1

    super bro, i got 1st approach and was scratching my head to optimize it , watched last method twice now i understood🤩

  • @rishijain9081
    @rishijain9081 24 дня назад

    What an explaination man !! Optimal solution was really out of my imagination.

  • @CodingEnv
    @CodingEnv Год назад +3

    You are the soul and heart of DSA.

  • @chethanprabhu4475
    @chethanprabhu4475 Год назад +2

    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

    • @takeUforward
      @takeUforward  Год назад +11

      Yes re-rrecorded, the previous videos were for people who already knew dsa

    • @vivekpatial5614
      @vivekpatial5614 Год назад

      ​@@takeUforward Your teaching skills are on another level :)

  • @subhambachar
    @subhambachar Месяц назад

    just blew my mind, The OP optimal approach 🤯🚀

  • @cinime
    @cinime Год назад +4

    Understood! Amazing explanation as always, thank you very much for your effort!!

  • @jaydeepkarena
    @jaydeepkarena Год назад +3

    Great explanation Raj. Thanks for creating this videos. Keep making videos like this. I have just started your SDE sheet for learning DSA

  • @anushkachavan5065
    @anushkachavan5065 4 месяца назад

    You are my motivation to solve the dsa problems every day. Thanks for providing these videos, it helps a lot🔥

  • @FooBar-lb5wf
    @FooBar-lb5wf 3 месяца назад

    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.

  • @satyammishra6363
    @satyammishra6363 6 месяцев назад +2

    For Brute force based solution, I think we should int_min or float("-inf), instead of -1, because -1 can be entry in matrix

    • @unlockyourfinance
      @unlockyourfinance 4 месяца назад

      Exactly, even int_min can be in the array since -2^31 is the limit in question..

    • @MahaveerJanagouda
      @MahaveerJanagouda 2 месяца назад

      ​@@unlockyourfinance​ u both are right, but how to approach it then? i tried int_MIN+1 also , 191 test casepassed for the first approach

  • @RahulYadav-jk7um
    @RahulYadav-jk7um 2 месяца назад

    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! ❤

  • @smileyyyy7372
    @smileyyyy7372 2 месяца назад +1

    May god bless you striver 🙏🙏

  • @shivammotani
    @shivammotani Год назад

    IDK why I felt the older video was more good. Found that one to be better. Even optimal code for that one is easier to understand.

  • @AyushSharma-ye1xz
    @AyushSharma-ye1xz Год назад +4

    All videos are very helpful ❤️

  • @harharmahadev3115
    @harharmahadev3115 Год назад +41

    Plz complete it in July

    • @sw_2421
      @sw_2421 Год назад +2

      Ek saal pakad ke chal..

    • @cdnggk6455
      @cdnggk6455 Год назад +1

      ​@@sw_24212 saal bhai 😂 free k cheez ki yahi dikat hoti hai

  • @rashi1662
    @rashi1662 10 месяцев назад +4

    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;
    }
    }

  • @mehulthuletiya497
    @mehulthuletiya497 Год назад +15

    Timestamps
    00:40 : Problem statement
    01:49 : Brute-force solution
    05:13 : - Code
    06:28 : - Complexity
    07:10 : Better solution
    12:16 : - Code
    13:49 : - Complexity
    14:17 : Optimal solution
    23:24 : - Code
    29:02 : - Complexity

  • @AjayKumar-sx6qo
    @AjayKumar-sx6qo Год назад +1

    OMG !!! Awesome explanation.. Thanks Striver

  • @RaviKumar-sn6tu
    @RaviKumar-sn6tu 5 месяцев назад

    watched your video ...then others video...then again your video ...then finally understood it😇😇

  • @NaveenKumar-xq6ce
    @NaveenKumar-xq6ce Год назад +3

    Your consistency is admirable @Striver

  • @rohitraj5832
    @rohitraj5832 11 месяцев назад +1

    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;
    }
    }
    }

  • @AyushEditz-hs6pf
    @AyushEditz-hs6pf 2 дня назад

    my head hurts after watching the optimal approach

  • @roxk6344
    @roxk6344 Год назад +4

    I want to complete this series so badly but then i saw he hasn't uploaded maximum videos. Please start uploading videos brother

    • @someshmone
      @someshmone Год назад

      Is it possible for tier3 college student and non it student to learn dsa

    • @Codeitt
      @Codeitt 11 месяцев назад +1

      no doubt@@someshmone

    • @Codeitt
      @Codeitt 11 месяцев назад

      it is possible.@@someshmone

  • @CodewithKing360
    @CodewithKing360 2 месяца назад

    Optimal is also understood after watching it 4 times by using pen paper and seeing edge case about last step
    😅

  • @tridibeshmisra9426
    @tridibeshmisra9426 6 месяцев назад

    Very good algorithm .... the optimal one is really good. Thanks a lot, sir!

  • @AdarshKeshari-wc2uz
    @AdarshKeshari-wc2uz 2 месяца назад

    Understood after going through twice
    ❤❤
    Best explanation

  • @abdulrehmaan153
    @abdulrehmaan153 Год назад +2

    You just added one more #fan in your infinte length array ❤

  • @suyashshinde2971
    @suyashshinde2971 Год назад +2

    SDE Sheet: Day 1 Problem 1 Done!

  • @mgnfy-view
    @mgnfy-view 10 месяцев назад +1

    Beautiful explanation!

  • @NikitaSingh-kz2ii
    @NikitaSingh-kz2ii Год назад

    watched the video thrice to finally get optimal approach to my head.
    thankyou for the efforts🤗🤗🤗

  • @prathameshhatkar1911
    @prathameshhatkar1911 Год назад +3

    Great optimisation 🔥

  • @coolman2035
    @coolman2035 23 дня назад

    guys if you are on leetcode number is from INT_MIN(negative) to INT_MAX(positive) that is we cant take -1 for optimal therefore do it by taking 0

  • @culeforever5408
    @culeforever5408 10 месяцев назад +2

    understood😇

  • @palak443
    @palak443 10 месяцев назад +3

    in first approch if the matrix numbers range is of int then what unique value can we take?

  • @rahulkumar-wz9jc
    @rahulkumar-wz9jc 8 месяцев назад +3

    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.

    • @EvenHeisenbergIsCertainAboutMe
      @EvenHeisenbergIsCertainAboutMe 8 месяцев назад

      Take some variable like a,n,... Instead of -1

    • @rahulkumar-wz9jc
      @rahulkumar-wz9jc 8 месяцев назад

      @@EvenHeisenbergIsCertainAboutMe kind of string type variable u r telling

    • @kushagrajain9493
      @kushagrajain9493 7 месяцев назад

      @@EvenHeisenbergIsCertainAboutMe but alphabets will be stored as asci value, which are again integers

  • @MamtaGupta-xj1kb
    @MamtaGupta-xj1kb 3 месяца назад

    #Striver rocks & #takeUforward learner rocks 🤟👍

  • @hareshnayak7302
    @hareshnayak7302 5 месяцев назад

    Understood,Thank you striver for this amazing video.

  • @jeetdesaimusic
    @jeetdesaimusic 3 месяца назад

    Crystal Clear Explanation!

  • @user-tm1hu4cw3v
    @user-tm1hu4cw3v 3 месяца назад +3

    Bro i got confused to m*n and n*m matrix pls check confirm your better solution by the way mine was like storing pairs almost the same...

  • @juliechoudhary9582
    @juliechoudhary9582 Год назад

    Understood and it's Amazing How you teach.

  • @surabsebait5409
    @surabsebait5409 Год назад +1

    Understood, very clearly explained

  • @karanveersinghdeora5085
    @karanveersinghdeora5085 Год назад +1

    Understood very well

  • @imranmaniar8881
    @imranmaniar8881 3 месяца назад +1

    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;
    }
    }
    }
    }
    }

  • @soothinghumms7019
    @soothinghumms7019 Месяц назад

    in Leetcode brute force problem there is a test case having matrix value as -1 which may cause a problem while changing -1 values in the matrix

  • @ankithazra2005
    @ankithazra2005 3 месяца назад

    My god 😮😮. Never thought this could have 0(1) space complexity

  • @gyana67644
    @gyana67644 Год назад +2

    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🙏🙏🙏 .

    • @takeUforward
      @takeUforward  Год назад +3

      in 7 days java and python add ho jaega

    • @theonlyritik
      @theonlyritik Год назад

      @@takeUforward what if I do dsa in javascript. Does it really matters? To be honest?

  • @sunnyvlogs__
    @sunnyvlogs__ Год назад

    Literally Awesome Explanation , easiest way

  • @Manmohanshukla420
    @Manmohanshukla420 Месяц назад

    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.

  • @sounaksaha1455
    @sounaksaha1455 4 месяца назад

    One of the best optimal approach

  • @shaheenparween4326
    @shaheenparween4326 10 месяцев назад +1

    understood✔✔

  • @kirtipalve1772
    @kirtipalve1772 3 месяца назад

    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

  • @suraj_fusion
    @suraj_fusion Год назад +6

    The brute force approach will not work for integer matrix (in leetcode integer matrix is there) but it will work for binary matrix

    • @sahilkhan_cs50
      @sahilkhan_cs50 Год назад

      Why?

    • @sahilkhan_cs50
      @sahilkhan_cs50 Год назад

      It will work I think

    • @suraj_fusion
      @suraj_fusion Год назад +1

      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 .

    • @sahilkhan_cs50
      @sahilkhan_cs50 Год назад

      @@suraj_fusion yes got it...u can place it with a value in the extreme bounds

    • @suraj_fusion
      @suraj_fusion Год назад

      @@sahilkhan_cs50 It should logically work didn't try it though.

  • @prashantsharma7869
    @prashantsharma7869 7 месяцев назад

    best explaination ,just love it!!

  • @rishabhkala2115
    @rishabhkala2115 Год назад

    thanks for this explanation
    understood everything

  • @ruchikaahujaasm
    @ruchikaahujaasm 4 месяца назад

    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 !!!

  • @user-ik3qu5uy5e
    @user-ik3qu5uy5e 6 месяцев назад

    AMAZING SIR , UNDERSTOOD VERY WELL

  • @sreejitkar8044
    @sreejitkar8044 26 дней назад

    Hatss off brother🙌🙌

  • @HR-pz7ts
    @HR-pz7ts Год назад

    It was a little overwhelming but I understood everything just need to implement the last technique on my own without copy pasting.

  • @nishthaarora1907
    @nishthaarora1907 10 месяцев назад +2

    if i talk about brute force , how can i assign -1 in the matrix , because matrix can have value from -2^31

  • @YourCodeVerse
    @YourCodeVerse 10 месяцев назад +1

    Understood✅🔥🔥

  • @priyotoshsahaThePowerOf23
    @priyotoshsahaThePowerOf23 Год назад

    First code in spreadsheet and got some feel🥵😱❣❣

  • @AniketKumar-hf2bo
    @AniketKumar-hf2bo 11 часов назад

    Understood ❤❤❤❤

  • @user-yw6zj3ir8p
    @user-yw6zj3ir8p 6 месяцев назад

    crystal clear approach

  • @ddevarapaga5134
    @ddevarapaga5134 Месяц назад

    wow mindblown amazing understood bro

  • @shivamsingh-we7ek
    @shivamsingh-we7ek Год назад +1

    for the element which are not in markRow and markCol , and that are -1 so when we go to mark them as zero we will ended up marking them also zero ????

  • @ankitasharma3930
    @ankitasharma3930 11 месяцев назад

    thank you so much for such a great effort.

  • @shivamgurjar8979
    @shivamgurjar8979 3 месяца назад

    Such a great solution ✨

  • @ankush8243
    @ankush8243 7 месяцев назад

    Thank you so much🥰
    Very much helpful !!