Maximal square | Dynamic programming | Leetcode

Поделиться
HTML-код
  • Опубликовано: 3 фев 2025

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

  • @quirkyquester
    @quirkyquester 4 года назад +33

    "im not sure if you can understand this" and "I hope you are understanding this" haha, love these two quotes haha lol

  • @pl5778
    @pl5778 4 года назад +46

    this is probably the best visual explanation i've seen thus far. This is really excellent, the ability to break it down with such simplicity. Subscribed.

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

      Thanks :)

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

      Unbelievable 😌😌
      Subscribed :)

  • @charlesbickham6604
    @charlesbickham6604 4 года назад +10

    Man I dont how you get answers like this! Been doing the leetcode 30 day challenge with you. Love your way of thinking and understanding! Would love for you to do a video of when to use different approaches like backtracking, sliding window etc!

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

      I will do it soon bro....

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

    amazing explanation, thank you! I love how you walk us through your thought process and assume that we don't know what you are talking about.

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

      Yep I can't assume everyone to understand me completely 😅

  • @rishabhpathak9916
    @rishabhpathak9916 4 года назад +4

    The way u explain with examples and all sorts of intutions, helps a lot to understand complex algorithms and codes. Yhank you so much

  • @khan.mansoor
    @khan.mansoor 3 года назад +1

    So underrated but such a useful video with very clear explanation. I was able to solve a related problem after watching the video halfway. Thank you so much.

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

    Thank you this is the easiest explanation I've found so far on the internet for this problem.

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

    I have seen your previous videos too, you are one of the best when it comes to explaining solutions!

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

    This channel deserves more subscription. You explained very well . Thanks a lot!

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

    You are doing awesome job, i have refered other video where people just memorize and repeat, you understand and explain things. Very helpful. Keep doing the good work.

  • @mondayemmanuel191
    @mondayemmanuel191 3 года назад +6

    You've been a great help in my learning of DSA. Thank you.

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

    As always...the best explanation available on youtube.....Thanks a lot surya...
    TechDose, helping us learn DS and get jobs since 2019:)

  • @SujeetKumar-hh5jz
    @SujeetKumar-hh5jz 4 года назад +2

    Even beginners can understand DP , the way you explain is soo good.

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

    The Best Explanation of DP. Made it look so easy at the end !!

  • @avikmallick2493
    @avikmallick2493 3 года назад +2

    Tech dose has become my daily dose

  • @alperozdamar517
    @alperozdamar517 4 года назад +5

    Marvelous solution! I love DP programming and your thought process. Thank you.

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

    Your videos have been helping me out a lot. Just wanted to say thanks

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

    this was so simple, but finding a rectangle was so complicated, we literally converted that to a histogram for each row and calculated that. wish there was a simple dp like this for that problem.

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

    I was super confused how to solve the problem. You gave me a nice hint. Thanks.

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

    Best explanation of the problem. Hat's OFF......

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

    No words to appreciate ur xplaination.

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

    Clear Explanation ! 👌👨‍💻

  • @francesn7715
    @francesn7715 3 года назад +2

    You make the best explanation videos for leetcode, hands down. Please make a video for #31 Next Permutation

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

      Thanks. I will try to make it soon :)

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

    Nice Explanation you are doing great job.................:)

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

    You simplified the problem, very well explained. thanks!!

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

    Awesome thanks! By far a detailed explanation. Subscribed for more.

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

    Thanking you bro ..it is very much helpful to me in the views of placement ..keep posting bro..😍😍😍😍

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

      Sure will keep posting :)

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

    Ye pyaar nhi toh aur kya hai !
    Amazing video bro 👌

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

    Such a great programmer and the way of explanation is very nice we want more videos like this. sir try to make videos on recursion.

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

    If i get this in an interview I'm walking out...Great explanation though, 0% chance you could solve this without already knowing the solution or something similar in a 30 min interview. And i'm solid at DP

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

    1st row and 1st column is not necessary in dynamic 2d array. We can take a 2d dp array of same size as input and fill it's 1st row and 1 column separately by the elements same as present in input array and then we can follow the same approach as mentioned in the video to fill other rows and columns.

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

    Love the explanation man !!

  • @kanishkagupta5801
    @kanishkagupta5801 4 года назад +24

    This type of approaches are far beyond our thought. How to proceed for this kinda questions?

    • @evgeni-nabokov
      @evgeni-nabokov 4 года назад +12

      Practice, practice, sis... Train your sense of determining the problem is solved by applying DP (1 dimension or 2 dimensions, or even more), and then follow some formal steps: what is a sub problem, what is recurrent formula, what are the initial values, which way to fill the table where is the answer in the table.

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

      You will need a lot of practice to identify solution for these questions :)

    • @Siva-lx9dw
      @Siva-lx9dw 4 года назад

      @@techdose4u how to practice??

    • @toksdotdev
      @toksdotdev 3 года назад +5

      @@Siva-lx9dw Solve more questions and explore different solutions to a problem.
      Before solving a problem, you can try to come up with a solution (especially when your interview is far away).
      Also, once you see a solution to a problem, don't just move on. Analyze why the solution works the way it, and compare it to how you were approaching the problem in the first place (even though you never arrived at the solution).
      Analyze the data structures used, and question why that specific data structure was used, and if you could solve it with an alternate data structure.
      In summary, analyze solutions, and ask your self why it works the way it works. Try to code the implementation (this is also one way to learn deeply why a solution works).

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

    great job;
    but u could have done it with just m*n dp instead of m+1*n+1 dp by hard coding top row and left column as default values of original matrix avoiding confusion

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

    Good Problem hard to think !!

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

    Thanks for help with this question, your explainations are really good

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

    Nice explaination ...

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

    Hi TechDose, Congratulations on making such a beautiful video. You always succeed in making a deeper dive into the concept behind a solution.

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

    Awesome explanation.
    I don't know how can anyone dislike it.

  • @robinofautumn99
    @robinofautumn99 9 месяцев назад

    Thankyou for such an amazing video!!

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

    Awesome explain bror 👍👍

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

    Nice explanation sir...you are doing a great job

  • @aviligondagowtham1153
    @aviligondagowtham1153 4 года назад +11

    Sir can u tell more problems on dynamic programming concepts bcz so many placement questions asked on this basis

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

      I will make a separate video on common patterns in dynamic programming questions. This will help you figure out the approach more in depth.

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

      @@techdose4u Thanks! waiting for that video!

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

      Hi, @TECH DOSE Were you able to make this video? Sorry for pestering you, but it would be really helpful if someone could provide a link to this if he has made it.

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

    Really good explanation!! Thanks a lot!

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

    Very good explanation. Thank you!

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

    wowwwww what an explanation!!!!!!!!!

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

    Very well explained. Thanks!!

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

    Best explanation 🔥

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

    What a great explanation!!

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

    thanks a lot for explaning such a hard problem in a easy way:)

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

    Very nice explanation!!! Thank you!!

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

    Crisp and clear explanation. Thanks !
    All of your videos are of great help. If you would like I can contribute to Python solutions of your videos. Github repo will be a good idea.

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

      Nice .....I will create one....

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

    Thank you so much, nice explanation

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

    Great explanation

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

    GOD Level explanation

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

    very good and detailed solution, thanks

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

    Lit🔥🔥 great video!

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

    best explanation... thankyou

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

    Clean and clear explanation!

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

    I tried solving this problem using DFS and got TLE on last 3 test cases on LEETCODE as you said, but got accepted in GEEKSFORGEEKS 😜

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

      😅

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

      How did you solve using DFS???

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

      @@crimsoncad3230 I just checked if encountered 1, then dfs from that location and incresed the count diagonally and check whether all are 1s in that square
      here is my code
      bool isSafe(int i, int j, vector grid){
      if(i= grid.size() or j= grid[i].size())
      return 0;
      return 1;
      }
      int dfs(int i, int j, int count, vector grid){
      //check diagonal right is 1 or not
      if(!isSafe(i+1, j+1, grid)){
      return count*count;
      }
      //then check for max size of square formed
      for(int k=0; k ans ? curr : ans;
      }
      }
      }
      return ans;
      }

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

      should have memoized then

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

    good explanation!!!

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

    Hi, I don't think we needed a dp matrix of size [n+1]*[n+1]. I think dp[n][n] would have worked. BTW love your videos :)

  • @chris.w391
    @chris.w391 3 года назад +1

    Nice explain, thanks!

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

    very helpful video...

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

    hi , please could you tell me how to find the indexes where the square matrix begins ?

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

    Thank you. your video helped me solved the problem. But I feel I am lacking so much more. Can anyone please tell me how do I improve dynamic programming problems? Can you recommend any resources? These DP problems are lowering my confidence day by day.

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

      Don't be down. It's just a matter a time when you pick them. DP is always tricky 😅

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

      @@techdose4u Thanks for kind words :) I started leetcode with "30 days challenge" only. out of 26 problems I was able to solve only 16. Remaining times, I had to look for explanations and understand and submit. So I was looking for help if I need to prepare and understand something, before answering leetcode solutions.
      Bytheway your explanation is so crisp than many other foreigner leetcode videos. They directly jump to code.

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

      I don't find meaning in explaining code. The important thing is how you thought about that process. Practice is always good. 16 is better than 15. You did well. You will only improve from here on. So don't stop. Keep practicing :)

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

    can u explain why we take min from three directions??

  • @Pikachu10-19
    @Pikachu10-19 2 года назад

    Amazinggg sir 🙏🏻

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

    Now I knew when I saw the question that it's got something to do with DP. But I froze at the thought of coding a DP solution. What do you recommend to practice in/read for getting more confident?

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

      Both reading and practice will help. But the imp thing is to notice dp patterns, where to use and where not to. Observing is important.

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

      Thank you sir 😊

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

      Welcome :)

  • @Official-tk3nc
    @Official-tk3nc 4 года назад

    Great explanation brother proud to be an indian:)

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

    i also solved this question by dp, it seems easy for me...still i watch your all videos to get better understanding 😄😄

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

    great diagrams!

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

    Hi Sir,
    Your explanation is crystal clear. 😊
    Can you please tell me the name of Recording application?
    I tried to Google but didn't find any recorder as good as this.
    Reply reply 😊

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

    very well explained!

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

    Amzing Sir.

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

    well explained, thank you!

  • @SHASHANKRUSTAGII
    @SHASHANKRUSTAGII 3 года назад +2

    dp is very beautiful

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

    so talented bruhh

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

    Why we just checked dp [i-1][j-1] == 1 and not other two cells in if condition?

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

    awesome bro!

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

    please do provide lnks for JAVA solutions if possible, you are doing great job Thanks

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

    I had some hard time fully understanding the DP approach so I managed to come up with another way to solve it in O(Nrows * Ncols) time and O(Ncols) space. The idea is to reuse what we learned with largest rectangle of 1s in a matrix. Once we get a histogram, just look for a square with side equal to (1 + current maximum side). I may make a video of it on my channel

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

    ```
    class Solution:
    def maximalSquare(self, matrix: List[List[str]]) -> int:
    rows, cols = len(matrix), len(matrix[0])
    #---------------------------------------------------------------------------------------------------------------
    # Time complexity - O(rows*cols)
    def solve1():
    dp = [0] * (cols+1)
    area, prev = 0, 0
    for r in range(1, rows+1):
    for c in range(1, cols+1):
    temp = dp[c]
    if matrix[r-1][c-1] == '1':
    dp[c] = 1 + min(prev, dp[c-1], dp[c])
    area = max(area, dp[c])
    else:
    dp[c] = 0
    prev = temp
    return area**2
    #---------------------------------------------------------------------------------------------------------------
    # Time complexity (TLE without cache) - O((rows*cols)*(rows*cols))
    @lru_cache(None)
    def dfs(r, c):
    if r >= rows or c >= cols or matrix[r][c] == '0':
    return 0
    return 1 + min(dfs(r+1, c), dfs(r, c+1), dfs(r+1, c+1))
    def solve2():
    area = 0
    for r in range(rows):
    for c in range(cols):
    area = max(area, dfs(r, c))
    return area**2
    #---------------------------------------------------------------------------------------------------------------
    # Time complexity (ACCEPTED) - O((rows*cols)*(rows*cols))
    def solve3():
    area = 0
    for r in range(rows):
    for c in range(cols):
    if matrix[r][c] == '1':
    curr = 1
    flag = True
    while r + curr < rows and c + curr < cols and flag:
    for c_ in range(c, c+curr+1):
    if matrix[r+curr][c_] == '0':
    flag = False
    break
    for r_ in range(r, r+curr+1):
    if matrix[r_][c+curr] == '0':
    flag = False
    break
    if flag:
    curr += 1
    area = max(area, curr)
    return area**2
    ```

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

    Which would you suggest to learn dp?

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

      Pardon? I think you want to know what to learn. Keep practicing dp questions. As you practice, keep searching about each and reason for a step. In this way, you will learn dp.

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

    u r amazing.....thnku

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

    Nice explanation 🙂

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

    Mind blowing approch

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

    what if we had to find rectangle instead of square??

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

      Well, a rectangle can be thought of as combination of 2 or more squares in the matrix. So we have to find continuous ccurrences of 2s or 3s or any higher number in a row of this DP matrix.
      We may take a counter "c" initialised to the first occurrence of a number >=2 in each row. And then increment it by one as an when we find more continuous occurrences of that number in the same row. We compute the area of a possible rectangle in each row traversal and store this value in a max variable. The area can be calculated as: counter × largest

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

    Wow, thank you so much Sir ! I can understand it lol..

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

      Welcome :)

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

      @@techdose4u I found out that for a problem if I watched your video, I can remember the solution for a Longgg time, if watch other's video I will forget the solution the next day. 😂

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

      @@yitingg7942 keep watching our videos ❤️

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

    Sir how to do recursiveky

  • @sagarsingh-wb8ou
    @sagarsingh-wb8ou 2 года назад

    Hi , Can someone explain in first way how he is using dfs??

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

    Thanks sir :-)

  • @Analogue-India
    @Analogue-India 4 года назад +1

    Please sir, make a video on problem Game of two stacks of hackerrank

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

      I have added this problem in my already long todo list. Will make it as soon as I get time for sure.

    • @Analogue-India
      @Analogue-India 4 года назад

      @@techdose4u Thank you sir

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

    Why are you checking dp [i-1][j-1] == 1 in if condition ??

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

      Diagonal adjacent cell is being checked here

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

    Amazing

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

    Awesome

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

    I got this question on Amazon sde internship interview. Was not able to answer

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

      🤔

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

      @@techdose4u I appreciate what you have been doing for us. Please keep up this amazing job.

  • @TP-xt4sv
    @TP-xt4sv 3 года назад

    excellent.

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

    sir pls check this solution not working in interviewbit

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

    what is the intuition behind using "min" in these elements?

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

      The idea is to check if there are occurrences of 0s in any of the past adjacent cells. Say you are currently at i = 3, j = 4 of the above example.
      If you take min of past adjacent cells, and it comes out as 0 then it means that a 2×2 square matrix CANNOT be formed.
      If you have min as 1 that means 3×3 matrix of 1s cannot be formed, here only a 2×2 is possible.