LeetCode 525. Contiguous Array (Algorithm Explained)

Поділитися
Вставка
  • Опубліковано 11 жов 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

КОМЕНТАРІ • 53

  • @oscaropdyou
    @oscaropdyou 4 роки тому +10

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

  • @YNA64
    @YNA64 2 роки тому +5

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

  • @calfland79
    @calfland79 4 роки тому +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.

  • @Dan-tg3gs
    @Dan-tg3gs 4 роки тому +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 4 роки тому +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 4 роки тому

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

  • @visheshmangla2650
    @visheshmangla2650 4 роки тому +1

    Consider two indices i and j where i

  • @jhonnymoreira9551
    @jhonnymoreira9551 4 місяці тому

    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.

  • @Whaleshamu
    @Whaleshamu 5 років тому +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 4 роки тому +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.

  • @madhurbhargava
    @madhurbhargava 4 роки тому +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 4 роки тому

      Watch mine.. I think you might get help

    • @madhurbhargava
      @madhurbhargava 4 роки тому

      @@AnandPandeyHere ok...going to do that

    • @7urkm3n
      @7urkm3n 4 роки тому

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

  • @abulaman8713
    @abulaman8713 3 роки тому

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

  • @subhedarsanchay
    @subhedarsanchay 4 роки тому +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!

  • @ahjiba
    @ahjiba 4 роки тому +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

  • @abhishekkrishna5978
    @abhishekkrishna5978 6 місяців тому

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

  • @manjorysaran6582
    @manjorysaran6582 4 роки тому +4

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

  • @duyetpham7924
    @duyetpham7924 4 роки тому +1

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

  • @vinayprabhakar6925
    @vinayprabhakar6925 3 роки тому

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

  • @officialspock
    @officialspock 4 роки тому +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 4 роки тому

      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 4 роки тому +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 4 роки тому +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 4 роки тому

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

    • @officialspock
      @officialspock 4 роки тому

      @@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?

  • @ianchui
    @ianchui 5 років тому +5

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

  • @Mohit-nn9bt
    @Mohit-nn9bt 3 роки тому

    I liked how you explained what is binary :-)

  • @minarezkalla4403
    @minarezkalla4403 2 роки тому

    Thank you for amazing explaination

  • @SubramanyaChakravarthy
    @SubramanyaChakravarthy 4 роки тому +1

    Great explanation thank you

  • @DAN-iz9gi
    @DAN-iz9gi 4 роки тому

    Thank you! Great explanation!

  • @KEVINMBARUA
    @KEVINMBARUA 4 роки тому

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

  • @RishiRaj-dl1mg
    @RishiRaj-dl1mg 4 роки тому

    Can we solve this using bottom up dp???

    • @rezwanurrahman7090
      @rezwanurrahman7090 3 роки тому

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

  • @ishaanj8023
    @ishaanj8023 4 роки тому

    Thank you

  • @lavanya_m01
    @lavanya_m01 2 роки тому

    Brilliant!

  • @yilinhuang4509
    @yilinhuang4509 4 роки тому +1

    awesome

  • @saicharan8675
    @saicharan8675 4 роки тому

    You are the best

  • @kunalnegi18
    @kunalnegi18 3 роки тому

    Return No of times 0 occurs ×2

  • @raihanar5323
    @raihanar5323 2 роки тому

    Im the 25,000 th view , 500 th like