Contiguous Array - Leetcode 525 - Python

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

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

  • @MP-ny3ep
    @MP-ny3ep 8 месяцев назад +39

    What sets you apart from the other youtubers is the fact that you don't just explain the solution. You also explain why other methods don't work? why only this method works? and how exactly do we come up with this solution?
    This video was an amazing explanation . Thank you for that.

  • @thisisnotok2100
    @thisisnotok2100 8 месяцев назад +70

    I would never think of this without seeing it before.

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

      I think the idea is you are aware of the solution. Whether you came up with it on your own or not is not the point.

  • @NeetCodeIO
    @NeetCodeIO  8 месяцев назад +40

    What does everyone think of the new leetcode UI? Kinda annoying that they don't let you switch back to the old one anymore

    • @Ahmed.Shaikh
      @Ahmed.Shaikh 8 месяцев назад +5

      I started leeting in the new UI and accidentally switched to the OG UI once through some keyboard shortcut. I was terrified of the old UI. Glad I was able to switch back to the new one. It just depends on what you're used to.

    • @AnasSalah-q2p
      @AnasSalah-q2p 8 месяцев назад +2

      ya, it's better to be able to switch between them

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

      I immediately thought of a sliding window and tried that, thanks for explaining why that wouldn't work. That type of explanation, mentioning why a particular solution doesn't work in this case is really really helpful.

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

      I just got into LeetCoding recently so I'm cool with it. If I could try the old UI I would.

    • @chien-yucode992
      @chien-yucode992 8 месяцев назад

      Same here

  • @chinmaywalinjkar7340
    @chinmaywalinjkar7340 8 месяцев назад +16

    I signed my Apple summer internship offer letter yesterday, thanks a lot for all your help. I hope you continue doing what you do the best

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

      woah. the dream

  • @BRBallin1
    @BRBallin1 5 месяцев назад +1

    Very interesting approach. This is the TDLR for the comment section:
    1. Maintain a count variable as you iterate through array. For each 0 subtract count by 1 and add by 1 for each 1.
    2. Maintain a hash map in which you store the leftmost index for each count value and initialize it with 0: -1
    3. As you iterate through the array if the current count value already exists in the hash map, update the size with curr_idx - map[curr_count] if that value is greater than the greatest size so far

  • @GautamPanday-z9q
    @GautamPanday-z9q Месяц назад +2

    I would have died trying to solve this problem using sliding window if you hadn't explained why we can't use sliding window here. Thanks a lot Neetcode.

  • @Ftur-57-fetr
    @Ftur-57-fetr 3 месяца назад

    What a genuine mind, what a talent to explain far-not intuitive concepts in a clear, easy to grasp manner!!

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

    How have you not won awards for your work. Been using your solutions for years now, I love you neetcode🌸

    • @NeetCodeIO
      @NeetCodeIO  7 месяцев назад +1

      Thank you so much 🙏

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

    Hi Navdeep, thank you for your solution! It will be extremely helpful if you include the brainstorming notes too in the future videos. Thanks!

  • @alxolr
    @alxolr 8 месяцев назад +2

    These problems are beautiful and set apart top-level coders from the rest. Hope one day to be able to solve this kind of problems myself.

    • @llorensbf
      @llorensbf 8 месяцев назад +2

      this particular problem sets apart people that has spent a huge amount of time on leetcode and somehow got the intuition for this, from people who may well be great coders but don't grind leetcode

    • @alxolr
      @alxolr 8 месяцев назад +1

      @@llorensbf That's also true, i would not give this problem at an interview as it doesn't show much technique.

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

    Just find the prefixSum arr, add 1 if 1 or -1 if 0 or vice versa, use hashMap to compare prev values but don't store when the sum repeats otherwise it will overwrite, check the sum=0 case also

  • @anirbanhossen6170
    @anirbanhossen6170 8 месяцев назад +1

    leetcode- Replace Question Marks in String to Minimize Its Value. Solve this question in python in easiest way.

  • @rthnrj
    @rthnrj 8 месяцев назад +2

    Great solution as usual. 🔥
    But, hope the following might make it less complex:
    In simple terms, in the array, if you see
    1 -> preSum - 1
    0 -> preSum + 1
    Then, you just have to check two cases -
    1. If the preSum is 0 (equal number of 1s and 0s till that index) -> update result index
    2. If the preSum already exists -> check the difference - NOTE: Should not update the hashmap (we only want the first seen preSum to stay to maximise the result)
    If not both, we are seeing this preSum for the first time -> Add it to hashmap

    • @rthnrj
      @rthnrj 8 месяцев назад +4

      Code for better understanding:
      def findMaxLength(self, nums: List[int]) -> int:
      preSum = 0
      ans = 0
      d = {}

      for i in range(len(nums)):
      #calculating preSum
      if nums[i] == 0:
      preSum += 1
      else:
      preSum -= 1

      #if the preSum is zero -> equal number of 1s ansd 0s
      if preSum == 0:
      ans = max(ans, i + 1)

      #the other case is that we have seen it already
      elif preSum in d:
      ans = max(ans, i - d[preSum])
      #seeing this preSum for the first time
      else:
      d[preSum] = i
      return ans

    • @swaroopas5207
      @swaroopas5207 8 месяцев назад +1

      Perfect even i was thinking the same. Simple and easy to understand

  • @JensUwe-24
    @JensUwe-24 6 месяцев назад +3

    How are you supposed to know this without seeing it before :(

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

    The comments are a great addition, Navdeep Paji

  • @dp8jl
    @dp8jl 3 дня назад

    This is a very smart solution, I came up with a prefix sum solution which was basically convoluted O(N^2) solution

  • @CSwithSolve
    @CSwithSolve 8 месяцев назад +4

    Congrats on 100k 🎊

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

    It's a Prefix sum problem

  • @dynaspinner64
    @dynaspinner64 8 месяцев назад +2

    Congrats on 100k!

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

    I was struggling with visualising the solutions, Thank for such easy explanation.

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

    @NeetCodeIO, you can shift the sliding window.
    Suppose we have array = [1, 1, 1, 0, 0, 0].
    As the the maximum length of the array is 6. We can consider the length of the array as a limit. If the sliding window has 3 one's and 2 zero's, and the length of the array is 6 then we can definitely shift our right side by 1. But if we have 4 one' and 1 zero's, the shifting the right side by does not make sense. So we will shift the left side of the window by 1.

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

    Finally an explanation I understood perfectly. Thanks!

  • @vikrambalaji4126
    @vikrambalaji4126 5 дней назад

    bro thank you for explaining why sliding window doesnt work I love u

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

    I am in the final grade in Russian Highschool, and I actually saw this problem preparing for my informatics exam (it was not with 0s and 1s, it was the problem to find the longest window that has the largest sum that meets all the requirements, but the idea was also about to keep all the prefixes), it was the last problem in the previous year's exam. I hope I will not get something like this, because I wont be able to solve it under the pressure, haha

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

    Yes, please show me another solution where you use -1 and 1. I would really like to see him, in general, this is a very difficult task for me. Thank you for the video!

  • @k.k.harjeeth5422
    @k.k.harjeeth5422 29 дней назад

    1 ) change all 0's to -1's
    2 ) now the question changes to finding the longest length subarray whose sum is 0

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

    Honestly I didn't think that much but I was able to solve the question :p. My thought process was "how about I get the largest array with sum = 0 and how to do that? consider every 0 as a -1"

  • @LlamaBG
    @LlamaBG 8 месяцев назад +1

    sick, thanks for continuing to make these

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

    very smart. the solution is so clean

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

    Amazing explanation today, thanks

  • @rahulsihara8946
    @rahulsihara8946 8 месяцев назад +1

    it was great explanation, I do understand the solution after watching your video. But how coming up with such solution is not intuitive for me, could you please help with that in any way?

  • @spsc07
    @spsc07 8 месяцев назад +1

    hey @NeetCodeIO at 0:46 wont it be !n (factorial of n) rather than n^2 (n squared) ??

    • @dcvc619
      @dcvc619 8 месяцев назад +1

      n! is equal to n * n-1 * n-2 * .... * 2 * 1, while the solution at 0:46 is n + n-1 + n-2 + .... + 2 + 1 which is (n(n+1))/2 which we simplify to O(n^2)

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

    love the solution but I aint never coming up with this during an interview without prior knowledge.

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

    Hey @NeetCodeIO, won't it be a better solution to use double pointers one at index 0 (s) and another at index n -1 (e), and increment or decrement s and e depending on the number of 1es.
    if sum(array[s:e]) * 2 > (e - s + 1), s++ if array[s] == 1 or e-- if array[e] == 1
    else if sum(array[s:e]) * 2 < (e - s + 1) , s++ if array[s] == 0 or e-- if array[e] == 0
    else return array[s:e].
    let me know what you think.

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

    please make complete playlist with problem sovling using python in from any online patfrom

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

    yup

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

    This is so intelligent and I still can’t figure out how can you come up with this solution XD

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

    What if we count all zeroes and ones first time and will use sliding window by decreasing it from right and left. For example, we have 3 zeros and 5 ones. We will decrease the side where we have ones or zeros if we don't have ones from each side and each time update(decrease) zeros and ones values. Will it work?

  • @chien-yucode992
    @chien-yucode992 8 месяцев назад

    Amazing :D

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

    does it matter if the array starts with a 0?

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

    one day ill be like you

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

    please keep the comments NC !!

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

    censoring dislikes from leetcode is annoying AF

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

    how do people just come up with this stuff

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

    Include comments! Sometimes the hardest part is intuiting the solution, so seeing how you got from Sliding Window to this Prefix Sum is interesting.
    Glad to have your videos & roadmap as I prep for interviews. Thx a bunch.

  • @fieworjohn5697
    @fieworjohn5697 8 месяцев назад +1

    am i the only one who wasn't able to pass all the testcases with this?
    here's my code, just in case someone can help point out my mistake
    class Solution:
    def findMaxLength(self, nums: List[int]) -> int:
    zeros, ones = 0, 0
    diff_index = {}
    res = 0
    for i, n in enumerate(nums):
    if i == 0: zeros += 1
    else: ones += 1
    if ones - zeros not in diff_index:
    diff_index[ones - zeros] = i

    if ones == zeros:
    res = ones + zeros
    else:
    idx = diff_index[ones - zeros]
    res = max(res, i - idx)
    return res

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

      I guess you've already figured it out, but just in case you haven't, your code should be:
      if n == 0:
      zeroes += 1
      else:
      ones += 1
      Because you need to be checking if each element of the array is equal to zero, not if the index is zero.

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

      @@silverblade3377 Oh my. Thanks!

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

    People are still learning dsa? Whym