Count Square Submatrices with All Ones | Dynamic Programming | Rolling Hash | Leetcode

Поделиться
HTML-код
  • Опубликовано: 8 окт 2024
  • This video explains a very important programming interview problem which is to count the number of square sub matrices which are having all ones. This can be solved using many approaches. I have explained 2 approaches for this problem. The first method is by using Rolling Hash algorithm and the second method is the most optimal approach using dynamic programming. I have explained every intuition required to solve this problem step by step so that you can understand all the steps involved while using dynamic programming.At the end of the video i have explained the CODE walkthrough. CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
    LinkedIn: / surya-pratap-kahar-47b... LINK: gist.github.co...
    SIMILAR PROBLEMS:-
    Rolling HASH: • Rolling hash | Rabin k...
    Maximal Square: • Maximal square | Dynam...

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

  • @ayeshaadhikari6123
    @ayeshaadhikari6123 2 года назад +1

    Thanks a lot sir! I was about to stop the video and skip this question but then you said - " you might not understand now, keep following the video, you'll understand" and this was a perfect push! I did understand the solution after the first 15 minutes. Great thanks to you and your efforts!

  • @vishnukumavat7489
    @vishnukumavat7489 4 года назад +13

    I never observed the patterns the way you did. It is such a great observation pattern you followed here. Thanks a lot for this.

  • @agileprogramming7463
    @agileprogramming7463 4 года назад +16

    I cannot believe that this kind of content is for free!! Too good to be true

  • @infinitibucket
    @infinitibucket 3 года назад +1

    Who cannot understand this after you have explained it so crisp and clear!

  • @hasrat17
    @hasrat17 2 года назад +1

    beautiful solution ... And well explained

  • @manishsalvi2901
    @manishsalvi2901 4 года назад +7

    You put so much effort to make us understand. Learn a lot from you. Thanks 😊

  • @amitupadhyay6511
    @amitupadhyay6511 2 года назад +2

    Wow, I thought this will be pretty tough one, tried for like 45 mins but no good. Thanks to your video, the trick is super-easy and coding is even easier lol

  • @yitingg7942
    @yitingg7942 3 года назад +4

    Did it myself easily with O(1) space since I remembered you had a previous video about this square method. 😎😎

    • @techdose4u
      @techdose4u  3 года назад

      Great ❤️

    • @amitavamozumder73
      @amitavamozumder73 3 года назад

      could u tell how to do that,i could only reduce space to 2xRow size :(

  • @neerajpant4071
    @neerajpant4071 4 года назад +7

    It's always a good option to watch your videos than reading explanation in leetcode
    It's good approach u focus more on explaination and approacg rather the coding👍

  • @HimanshuKumar-xz5tk
    @HimanshuKumar-xz5tk 2 года назад

    Wonderful, finally understood why we doing what we doing to solve this

  • @crankyinmv
    @crankyinmv 4 года назад +1

    Thanks for that. I wound up doing a variation which mapped total # of 1s in a rectangle between 0,0 and the each cell. A little more involved, but it worked.

  • @AngadSingh97
    @AngadSingh97 4 года назад +3

    this was amazing, I had to see 2 other videos for this, but it was totally worth it.

  • @vivektyson
    @vivektyson 4 года назад +1

    Best explanation on this problem. Amazing sir. I watched a lot of videos on this problem but couldn't understand it. Thank you so much sir.

  • @parthshah6343
    @parthshah6343 4 года назад +1

    I solved it using the method you suggested for Maximal Square problem in the exact same way!

  • @Amit_Kumar_1999
    @Amit_Kumar_1999 4 года назад +1

    Solved it with dp. But I will definitely try it with rolling hash today. Thanks for idea.

  • @sonu-ou2hx
    @sonu-ou2hx 4 года назад +1

    The way you explain the problem along with its solution is amazing .
    Thanks a lot Sir.

  • @mohdfarman3470
    @mohdfarman3470 2 года назад

    Sir your explanations are best ,please make more videos like these.

  • @setYourHandleHttp404
    @setYourHandleHttp404 4 года назад +1

    You provide the best way to convey the logic!

  • @Bruner4
    @Bruner4 4 года назад +1

    I skipped first 2 for cycles and scrolled the whole matrix adding 1 in case, in this way:
    if (matrix[i][j] == 1)
    ausMat[i][j] = Math.min(ausMat[i - 1][j - 1], Math.min(ausMat[i - 1][j], ausMat[i][j - 1])) + 1;
    Computationally speaking is the same thing I guess, right?
    Btw amazing videos man, keep going!!

  • @spetsnaz_2
    @spetsnaz_2 4 года назад +2

    I was able to get near the solution by using your concept explained in the problem Maximal Square. Using that I had the idea to take the min of all three sides + 1 😜
    BTW this problem can also be solved in-place

  • @MohitBhagwani94
    @MohitBhagwani94 4 года назад +2

    Sir your explanation is awesome. There is nothing wrong in your code just one refinement.
    We don't need an extra vector DP to store the value, we can change the matrix value in-place. My code is in java, please do let me know if it's wrong and what problem I'll face if I change the matrix value?
    class Solution {
    public int countSquares(int[][] matrix) {
    int m = matrix.length;
    int n = matrix[0].length;
    int total=0;
    for(int i=0;i0){
    matrix[i][j] = Math.min(matrix[i-1][j],Math.min(matrix[i][j-1],matrix[i-1][j-1]))+1;
    }
    total +=matrix[i][j];
    }
    return total;
    }
    }
    Thanks in advance.

  • @rohithv63
    @rohithv63 3 года назад +1

    Great explanation and the idea helped me to code on my own. Very much helpful

  • @jameslin9845
    @jameslin9845 3 года назад +1

    Very easy to understand. Thanks

  • @soumilkhandelwal4388
    @soumilkhandelwal4388 4 года назад +1

    very nice... Usually, try to solve questions on my own... but couldn't get the logic even after 3 hours, Thanks for such a nice explanation. Now please tell how did you come up with this approach!! the thought process, because these type of questions usually goes in DFS or some graph-related stuff. How did you get to this solution buddy?

    • @techdose4u
      @techdose4u  4 года назад +1

      I have done many such questions so din't wait to think on how to solve 😅

    • @soumilkhandelwal4388
      @soumilkhandelwal4388 4 года назад

      @@techdose4u lol ... ohk ... Just wanted to know the thought process... In future whenever you solve a tricky question please provide your thought process... because that is the actual thing which really helps.

  • @BaishaliGhosh13
    @BaishaliGhosh13 4 года назад +1

    Your explanations are really great. Thank you for your help. Rolling Hash concept is really great and I have learnt it from your channel.

  • @wentingsong9435
    @wentingsong9435 4 года назад +3

    Looking forward to your videos everyday, you gave me so much motivation, thanks!

  • @luzengyuan5326
    @luzengyuan5326 3 года назад +1

    very clear explanation. thanks

  • @joseph2073
    @joseph2073 3 года назад

    thanku so much for the explanation man. u made it so easy. god bless u ! :)

  • @shivamahuja840
    @shivamahuja840 3 года назад

    hi sir your all videos are the best sir can you please make videos on all types of questions of letter combination of phone like one with only recursion,then with backtracking and then with dp it will be a good topic for everyone and its not covered yet anywhere..

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

    Thank you. Can you please add more why we multiple by min(N,M) at 4:29?

  • @amanbansal97
    @amanbansal97 4 года назад +17

    8:40 I hope you didn't understand it too well 😂

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

    Sir , instead of considering the matrix cell as bottom right corner of the square , can we consider it as top left corner and then start solving the problem from last cell to the first cell ?

  • @kushgupta1187
    @kushgupta1187 4 года назад +2

    Thanks for taking time every day and uploading video.. Just a suggestion when explaining dp solution first share recursive equation otherwise we would not be to get how we reach to equation inside loop and every new question related to dp will become almost impossible for us to solve as we don't know how to form recursive equation.

    • @techdose4u
      @techdose4u  4 года назад +8

      This is not a typical recursion question hence was difficult to solve using direct DP. I have already explained recursion formulation and its conversion to DP solution as well. I will do this in future as well. But this was not a typical recursive problem. So, I went with examples.

    • @kushgupta1187
      @kushgupta1187 4 года назад

      Thanks for replying.. May be i am wrong but i think every dp has recursive equation. We just minimizes call by storing previous result in memory.
      Thanks anyways for detail explanation of approach :)

  • @deepaligarg2978
    @deepaligarg2978 2 года назад

    Simply wonderful

  • @nikhil_squats
    @nikhil_squats 4 года назад

    aap insaan nhi farishta ho, keep making these videos really helpful
    please increase the frequency of making videos

  • @papayasprout
    @papayasprout 2 года назад

    Thanks man. Very well explained

  • @hemantsinghmanral8898
    @hemantsinghmanral8898 4 года назад +1

    Nice explanation...thanks for posting problems everyday

    • @techdose4u
      @techdose4u  4 года назад

      Will continue everyday this month.

  • @garumugam4480
    @garumugam4480 4 года назад +1

    Nice explanation. I have to learn dynamic programming..

  • @christophermg1986
    @christophermg1986 3 года назад

    Loved this Dose for dp :)

  • @paragroy5359
    @paragroy5359 4 года назад +2

    Too good explanation sir, i can't imagine getting all this for free..Thanks a lot sir

  • @nurshuvo3854
    @nurshuvo3854 2 года назад

    Great tutorial.

  • @pranjalsharma499
    @pranjalsharma499 4 года назад

    Very well Explained

  • @ajaywadhwa3398
    @ajaywadhwa3398 4 года назад +1

    Awesome Explanations !! Really helped alot.

  • @rakshitsahu3417
    @rakshitsahu3417 3 года назад +1

    best explanation!

  • @akshbansal7715
    @akshbansal7715 4 года назад +2

    If we do the operations in the original matrix itself then space complexity would be O(1), right?

    • @techdose4u
      @techdose4u  4 года назад

      Yes correct. It can be done inplace.

  • @aswathsrimarir2501
    @aswathsrimarir2501 4 года назад

    How do we need to approach if they ask us not only to consider square matrices.? and count matrices of any dimensions?

  • @avinabhchambial6283
    @avinabhchambial6283 4 года назад +1

    thanks man! you're a life saver :)

  • @mohdfarhanahmed9416
    @mohdfarhanahmed9416 3 года назад

    Can we get the java code as well in the upcoming videos?

  • @ShubhamMahawar_Dancer_Actor
    @ShubhamMahawar_Dancer_Actor 4 года назад +1

    Great man,you are really doing a great job

  • @youtubealgorithm2128
    @youtubealgorithm2128 2 года назад

    thanks a lot bro for this video

  • @tushardhote6393
    @tushardhote6393 4 года назад +1

    Your explanation is amazing.. 👌🏻👌🏻👌🏻
    Please upload explanation of leetCode problem "Surrounded regions". It will be appreciated 💯💯💯

    • @techdose4u
      @techdose4u  4 года назад +7

      Will do more questions later bro.....feeling exhausted by putting video everyday :(

    • @ds_world2468
      @ds_world2468 4 года назад

      class Solution {

      public void bfs(char[][] board, boolean visited[][], int rows, int cols, int i, int j){
      if(i < 0 || i == rows || j < 0 || j == cols || visited[i][j] || board[i][j] != 'O')
      return;
      visited[i][j] = true;
      bfs(board, visited, rows, cols, i-1, j);
      bfs(board, visited, rows, cols, i+1, j);
      bfs(board, visited, rows, cols, i, j-1);
      bfs(board, visited, rows, cols, i, j+1);
      }

      public void solve(char[][] board) {
      if(board.length == 0 || board[0].length == 0)
      return;
      int rows = board.length, cols = board[0].length;
      boolean visited[][] = new boolean[rows][cols];

      for(int c = 0; c < cols; c++){
      if(board[0][c] == 'O')
      bfs(board, visited, rows, cols, 0, c);
      if(board[rows-1][c] == 'O')
      bfs(board, visited, rows, cols, rows-1, c);
      }
      for(int r = 0; r < rows; r++){
      if(board[r][0] == 'O')
      bfs(board, visited, rows, cols, r, 0);
      if(board[r][cols-1] == 'O')
      bfs(board, visited, rows, cols, r, cols-1);
      }

      for(int i = 1; i < rows-1; i++){
      for(int j = 1; j < cols-1; j++){
      if(board[i][j] == 'O' && visited[i][j] == false)
      board[i][j] = 'X';
      }
      }

      }
      }

  • @thiyagarajanr225
    @thiyagarajanr225 4 года назад +3

    Such a great insight! Bro

  • @siddhartha.saif25
    @siddhartha.saif25 4 года назад +1

    very nice! thanks!

    • @techdose4u
      @techdose4u  4 года назад

      Welcome :)

    • @siddhartha.saif25
      @siddhartha.saif25 4 года назад

      @@techdose4u the instructions are so very clear. there are lot of things to learn from you. thank you for all the great work you are doing.

  • @fiascoDazzle
    @fiascoDazzle 3 года назад

    Thanks for uploading great videos. Is there a way we can extend the 2nd method to calculate the count for submatrices of 1s in a matrix of 1 and 0?

  • @hemantsinghmanral8898
    @hemantsinghmanral8898 4 года назад +1

    can you please add a playlist of questions based on graphs i find them very hard

  • @princeakhil208
    @princeakhil208 4 года назад +1

    Can you please do a playlist on graph problems..

    • @techdose4u
      @techdose4u  4 года назад

      Yes will do it as soon as leetcode challenges end.

  • @gorakhkumargupta491
    @gorakhkumargupta491 2 года назад

    Amazing content

  • @algorithmimplementer415
    @algorithmimplementer415 4 года назад +1

    This channel rocks!!

  • @dovyraj1272
    @dovyraj1272 4 года назад +1

    sir u r great

  • @desihiphop4998
    @desihiphop4998 2 года назад +1

    Dope solution man !!!!!!!!!!1

  • @codingwithanonymous890
    @codingwithanonymous890 2 года назад

    Sir competitive nhi ho raha...can i be good software devloper?

  • @HarshGupta-if2xg
    @HarshGupta-if2xg 4 года назад +1

    please upload problem on a daily basis.

    • @techdose4u
      @techdose4u  4 года назад +1

      I am uploading on daily basis only from the last 2 months. I think you miss the videos. I will not continue on daily basis from next month onwards.

  • @sumanthsaadam4522
    @sumanthsaadam4522 4 года назад +1

    Problems on stacks and queues please

    • @techdose4u
      @techdose4u  4 года назад

      Please check stack and queue playlist.

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

    Can you please post the rolling hash 2d algorithm in Java?

  • @techsquare6708
    @techsquare6708 3 года назад +1

    awesome

  • @ruchivora1190
    @ruchivora1190 4 года назад

    great explanation!

  • @sammyali2938
    @sammyali2938 4 года назад +1

    I tried to write the code. It did NOT work. I checked every thing, it does not work.

    • @techdose4u
      @techdose4u  4 года назад

      Post your code in New comment on this video to get help.

  • @cyruspassi457
    @cyruspassi457 4 года назад +1

    Your Linkedin says "This profile is not available".

    • @techdose4u
      @techdose4u  4 года назад +1

      I will check it. Maybe the link is broken.

  • @manjunathm3384
    @manjunathm3384 4 года назад +1

    Can please solve this code using python or c

    • @techdose4u
      @techdose4u  4 года назад +1

      It is not possible for me but I can recommend you 2 things. 1st, you can get any language solution by just going to the leetcode problem discussions. 2nd, you can see if somebody has posted the code. I will soon create a repo where everyone can contribute and so it will be much easier.

    • @manjunathm3384
      @manjunathm3384 4 года назад

      @@techdose4u thank you sir

  • @ds_world2468
    @ds_world2468 4 года назад +1

    First View... I had used the same approach...Nice Explanation

  • @b.sainathreddy4567
    @b.sainathreddy4567 4 года назад +1

    this is same as maximal square leetcode problem

  • @satanshuag
    @satanshuag 2 года назад

    in start i as : | ..... Till the end --> : )

  • @symbol767
    @symbol767 2 года назад

    Why do companies ask these ridiculous questions, holy hell..