LeetCode 525. Contiguous Array (Algorithm Explained)

Sdílet
Vložit
  • čas přidán 6. 09. 2024
  • The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
    Join my free exclusive community built to empower programmers! - www.skool.com/...
    Preparing For Your Coding Interviews? Use These Resources
    --------------------
    (My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.d...
    AlgoCademy - algocademy.com...
    Daily Coding Interview Questions - bit.ly/3xw1Sqz
    10% Off Of The Best Web Hosting! - hostinger.com/...
    Follow Me on X/Twitter - x.com/nickwhit...
    Follow My Instagram - / nickwwhite
    Other Social Media
    ----------------------------------------------
    Discord - / discord
    Twitch - / nickwhitettv
    TikTok - / nickwhitetiktok
    LinkedIn - / nicholas-w-white
    Show Support
    ------------------------------------------------------------------------------
    Patreon - / nick_white
    PayPal - paypal.me/nick....
    Become A Member - / @nickwhite
    #coding #programming #softwareengineering

Komentáře • 53

  • @calfland79
    @calfland79 Před 4 lety +22

    It is definitely a medium problem. counts.put(0, -1) only this line could kill 90% people if they have never study this problem before.

  • @oscaropdyou
    @oscaropdyou Před 4 lety +9

    If there is confusion on why count "0" is initialised with -1 index, this is because before we start, we start with count of 0. If we get lets say [0,1], then it would go to -1 and 0. And as we have initialised first index of 0 as -1. And length would be -1 to 1(current index when count reached 0).

  • @Dan-tg3gs
    @Dan-tg3gs Před 4 lety +36

    i feel that this solution was hard to come up with without already seeing some kind of pattern of this question before. When I saw the solution, I understood it, but I'm wondering if these types of questions will get intuitive with more practice? Is this considered DP?

    • @extravaganza69
      @extravaganza69 Před 4 lety +14

      I've got the same problem. Basically, many solutions to medium+ problems are not really trivial or intuitive as they usually are based on some external assumption or characteristic of data. I think it's almost impossible to get it right every time, but definitely, it's possible to develop some kind of intuition as there is a finite amount of categories these problems belong to. Stay cool and positive! Cheers

    • @BrijeshPatelEngineer
      @BrijeshPatelEngineer Před 4 lety

      ​@@extravaganza69 yeah i think finite amount of categories you mentioned should be documented somewhere. It would be helpful to look at initially when we get stuck !!!
      Agree, should stay cool. Revisit the problems every interval to keep hang of it.

  • @YNA64
    @YNA64 Před 2 lety +3

    Nick : "It's not that hard, it could be easy even."
    Also Nick Submissions : Multiple Rejection
    Cmon dude...

  • @jhonnymoreira9551
    @jhonnymoreira9551 Před 3 měsíci

    Thanks for clearing the problem and going over the solution. I was having a hard time understanding the concept of `contiguous` (non-native english speaker), and the longer example plus your code helped a lot.

  • @visheshmangla2650
    @visheshmangla2650 Před 4 lety +1

    Consider two indices i and j where i

  • @madhurbhargava
    @madhurbhargava Před 4 lety +19

    Did not really get why this video was created if the whole idea was to just read the solution from the solutions section.

    • @AnandPandeyHere
      @AnandPandeyHere Před 4 lety

      Watch mine.. I think you might get help

    • @madhurbhargava
      @madhurbhargava Před 4 lety

      @@AnandPandeyHere ok...going to do that

    • @7urkm3n
      @7urkm3n Před 4 lety

      Cuz, he cares more about views. Basically he said and read the same thing on the solutions, and copy the solution code.

  • @Whaleshamu
    @Whaleshamu Před 4 lety +7

    i understand that we are putting -1 for 0, coz we have not seen 0 yet. But why we are initially putting 0 inside map?

    • @shreejitnair2174
      @shreejitnair2174 Před 4 lety +12

      Think about a simple condition where your array is [0,1] this would not update maxlen when the count becomes 0. Adding this check gives you (index - (-1)) so your maxlen = 2 now. Don't think about adding this condition first. Think about a test case or a counter example that breaks your logic without this condition. Adding this condition would appear natural at that point.

  • @manjorysaran6582
    @manjorysaran6582 Před 4 lety +4

    u r really good! how did you practice and for how long before you could code like this?

  • @subhedarsanchay
    @subhedarsanchay Před 4 lety +15

    Hey Nick, great video. Thanks for all of your hard work. Just have a minor suggestion. I have seen a bunch of your videos and you almost tend to size the problem by saying "oh this is an easy problem" or "this problem is super simple". This might discourage the viewer into thinking they can't even solve these easy problem. And believe me, most viewers who are watching your videos are the folks who could not solve this problem and hence are looking to understand the solution. I'd appreciate if you could stop doing that. Let me know if I am being too sensitive. Again, I am your youtube subscriber and thank you!

  • @vinayprabhakar6925
    @vinayprabhakar6925 Před 3 lety

    Can u plz tell why we added 0,-1 into our hashmap before going through the array didn't get that part

  • @abhishekkrishna5978
    @abhishekkrishna5978 Před 5 měsíci

    this is a fabulous way of looking at the problem. i wouldn't have comeup with this solution.

  • @duyetpham7924
    @duyetpham7924 Před 4 lety +1

    i try many ways before view video, this is great. It is simple and power, thank you.

  • @abulaman8713
    @abulaman8713 Před 3 lety

    I thought of someway of substracting the number of zeros and ones to arrive at the solution....,
    Wow man, love your brains

  • @ahjiba
    @ahjiba Před 4 lety +1

    initially thought of this problem as max contiguous subarray equals k, where k = 0 and all 0s are converted into -1, hope that helps anyone thinking through this problem

  • @ianchui
    @ianchui Před 4 lety +5

    awesome work! helped me very much. I did not enjoy going through that animation at all lmaoo

  • @SubramanyaChakravarthy
    @SubramanyaChakravarthy Před 4 lety +1

    Great explanation thank you

  • @Mohit-nn9bt
    @Mohit-nn9bt Před 3 lety

    I liked how you explained what is binary :-)

  • @RishiRaj-dl1mg
    @RishiRaj-dl1mg Před 4 lety

    Can we solve this using bottom up dp???

    • @rezwanurrahman7090
      @rezwanurrahman7090 Před 3 lety

      I think, without hashmap dp will not be possible in O(n) time

  • @officialspock
    @officialspock Před 4 lety +3

    I don't understand the example, why is [0,1] maxlen is 2, and [0,1,0] maxlen is still 2, I had a look at the discussion and get more confused... I hope someone can explain this properly before I can formulate a logic to solve this.
    [0,0,0,1,1,2,1] = 6 // other numbers than 0 and 1 are ignored
    [0,1,0,1] = 4 // so this is 4, why?
    [0,1,0,1,1] = 4 // this is also 4... so there is some symetry condition??
    [0,0,1,0,0,0,1,1] = 6 // this is 6?? I do not see why. I would have expected 4

    • @damudaran3765
      @damudaran3765 Před 4 lety

      i have the same confusion bruh . this bastard in this video explain shit . he always rush up never cares about the viewers understanding worst experience.

    • @anybody_y
      @anybody_y Před 4 lety +6

      took a a lot of re-read the problem statement to clear up my confusion, to answer you:
      [0,0,0,1,1,2,1] i believe you meant [0,0,0,1,1,0,1] , it is 6 because starting from the 2nd index [0,0,1,1,0,1] it is contiguous because this subarray has exactly the same number of 0 as to 1, 3 each.
      [0,1,0,1] = 4 because the whole array has same number of 0s and 1s, so the length of the array (4) is the longest subarray
      [0,1,0,1,1] = 4 because up to the first 4 elements, the subarray has exactly the same number of 0s and 1s, therefore it is 4.
      [0,0,1,0,0,0,1,1] = 6, from the 3rd index to the end, it has the same number of 0s and 1s therefore it is 6.

    • @anybody_y
      @anybody_y Před 4 lety +3

      @@damudaran3765 instead of blaming others of not explaining in the level of detail that you are expecting, you could be more helpful by explaining what exactly you are confused with and ask for help around that confusion. you need to help others to understand what you don't for others to help you.
      your comment come across to me that you have spent no effort of trying to understand your own confusion.

    • @hjr834
      @hjr834 Před 4 lety

      @@anybody_y great explanation man, I've missed something like that while going through the statement of the problem

    • @officialspock
      @officialspock Před 4 lety

      @@anybody_y thanks for your explanation, so basically, contiguous means same count of 1 and 0? The longest contiguous same count of 1 and 0?

  • @minarezkalla4403
    @minarezkalla4403 Před 2 lety

    Thank you for amazing explaination

  • @DAN-iz9gi
    @DAN-iz9gi Před 4 lety

    Thank you! Great explanation!

  • @ishaanj8023
    @ishaanj8023 Před 4 lety

    Thank you

  • @lavanyam3224
    @lavanyam3224 Před 2 lety

    Brilliant!

  • @yilinhuang4509
    @yilinhuang4509 Před 4 lety +1

    awesome

  • @saicharan8675
    @saicharan8675 Před 4 lety

    You are the best

  • @KEVINMBARUA
    @KEVINMBARUA Před 4 lety

    bruh! you are the shiznit! that is all!!!!!!

  • @kunalnegi18
    @kunalnegi18 Před 3 lety

    Return No of times 0 occurs ×2

  • @raihanar5323
    @raihanar5323 Před 2 lety

    Im the 25,000 th view , 500 th like