Continuous Subarrays | Leetcode 2762

Поділитися
Вставка
  • Опубліковано 14 гру 2024

КОМЕНТАРІ • 72

  • @sivapradeep977
    @sivapradeep977 День тому +21

    Suddenly my brain is not braining😂😂

  • @rutikbhanushali957
    @rutikbhanushali957 День тому +10

    Moving left pointer back to avoid duplicate was a bit tricky...

  • @Anikait-h3d
    @Anikait-h3d День тому +7

    Loving the explanation Hating the problem

    • @techdose4u
      @techdose4u  День тому

      hahahaaha 🤣

    • @heybeachMIN
      @heybeachMIN День тому +1

      @@techdose4u Hi, your python code is not right. There is a small error there, at the end after the loop "for" you need to add to win_size + 1, that is, win_size = r - l + 1. I'm not exactly sure, but maybe this is because we don't add to the count without the condition max - min > 2 and always in the end we need to add to count and there the count in the loop starts from zero, so + 1. Sry for engl)

    • @techdose4u
      @techdose4u  День тому

      yea.
      AI is trash 😮‍💨

  • @ishanbhatt6067
    @ishanbhatt6067 23 години тому

    great call to include the complexity analysis! i came up with a similar solution but rejected it because i thought moving left pointer back would cause the complexity to become N-squared!
    thanks :)

  • @freecourseplatformenglish2829
    @freecourseplatformenglish2829 День тому

    Excellent expalination, I too comeup with the exact approach but struggle to code it. Thanks man.

  • @MP-ny3ep
    @MP-ny3ep День тому

    Very beautifully explained !

  • @jaatharsh
    @jaatharsh День тому

    good explanation as always, many thanks

  • @smitshah2113
    @smitshah2113 День тому

    Nice explanation. Thanks. Just one tiny bug: when moving back left, we need to ensure we are not moving out of the array

    • @techdose4u
      @techdose4u  День тому +1

      but that condition wont arise coz we must have a break point otherwise the right pointer wouldnt stop!

    • @smitshah2113
      @smitshah2113 День тому

      @@techdose4u Ah, yes... the fact that the right pointer did stop means that there are two indices for which the subarray is not continuous. Hence we would stop moving left before going out of bounds. Thanks :)

    • @techdose4u
      @techdose4u  День тому

      yep :)

  • @gireeswar18
    @gireeswar18 День тому +1

    Nice explanation, may I know which blackboard you are using?

  • @asgarantony
    @asgarantony День тому

    Good explanation Sir.

  • @vishalgadade1585
    @vishalgadade1585 День тому

    As you are moving pointer again to left when we found any fault
    Isn't it is disturbing cpu cycle? Because it is making knot of pointers during computation
    Can we optimize this knot ?

    • @techdose4u
      @techdose4u  День тому

      can you please explain with an example

  • @dieformusic1705
    @dieformusic1705 День тому

    sir can i use priority queues to maintain min and max values so as to avoid the left movement of L pointer where i can update L pointer to the index choosen from the priority queues after checking the condition?

    • @techdose4u
      @techdose4u  День тому

      Yea you can take priority queue but you will need 2 heaps. One for min and one for max

  • @ujjwal_7531
    @ujjwal_7531 День тому

    wonderful explain

  • @BigStin-0
    @BigStin-0 День тому +3

    God damn, my head hurts hahaha

  • @lakithakare7387
    @lakithakare7387 День тому

    Thank you

  • @devmahad
    @devmahad День тому

    thanks :)

  • @algorithmsanddatastructure3701

    i think we can adjust the left pointer also simply by maintaining the index and updating the index if the same number occuring multiple times

    • @techdose4u
      @techdose4u  День тому

      yes.
      But the candidates may differ and so you will endup maintaining all frequencies :)

    • @algorithmsanddatastructure3701
      @algorithmsanddatastructure3701 День тому

      @techdose4u i am thinking of maintaining the index of only max and min,so whenever difference is greater than the given number, it will be because max and min, so accordingly we can move the left pointer either to min or max which ever was present in the subarray

  • @Abhishek-e6d2c
    @Abhishek-e6d2c День тому

    what an explanation!!!

  • @vidhyarthisunav
    @vidhyarthisunav День тому

    Your Python code in the link need minor enhancement. n = len(nums) should be used instead of right pointer to calculate the final number of subarrays to be addes outside the loop on line number 126. It should be win_size = n - left instead of win_size = right - left.

  • @nicolaswolyniec1354
    @nicolaswolyniec1354 День тому +1

    the python solution will give you a wrong answer. After finishing the loop, you can not use the right variable because the value is n-1
    win_size = len(nums) - left
    count += (win_size * (win_size + 1)) // 2

  • @thetruebluesingaporeankid
    @thetruebluesingaporeankid День тому

    could explain why the n*(n+1)/2 come about?

    • @techdose4u
      @techdose4u  День тому

      You can enumerate and count.
      it will be this always:)

  • @SzrIsTaken786
    @SzrIsTaken786 День тому

    Had the sliding window part down, the maximum and minimum threw me off a bit but the formula is the part where I'm thinking "how the heck??"

    • @techdose4u
      @techdose4u  День тому +1

      yea, it was not your daily sliding window :o confusing

  • @LinhHoang-ml1qo
    @LinhHoang-ml1qo День тому

    Hi sir, I think exactly the time complexity is O(2N) in the worst case l and r is both come to the last of index. Please confirm if my comment is false.Thank you so much and have a good day

    • @techdose4u
      @techdose4u  День тому

      Anything is O(N)
      Thankfully we dont need to consider constants :)

  • @pravyn350
    @pravyn350 День тому

    This guy ❤

  • @vikasvoruganti7490
    @vikasvoruganti7490 День тому

    Time to practice more sliding window problems 😢

  • @sailendrachettri8521
    @sailendrachettri8521 День тому

    And i was just counting subarrays

    • @techdose4u
      @techdose4u  День тому

      Need to subtract the redundant ones :)

  • @nileshbhanot2776
    @nileshbhanot2776 День тому

    Your python does not submit on leetcode and provides wrong output. Kindly, please check

    • @techdose4u
      @techdose4u  День тому +1

      omg
      How can AI take jobs if they can’t even convert codes 🥹

    • @satguy8481
      @satguy8481 День тому

      @nileshbhanot2776 betaa khud se code likh le cheating krne aa gya moo utha ker. Approach dekh code nhi

    • @nileshbhanot2776
      @nileshbhanot2776 18 годин тому

      @@techdose4u Haha I tried to convert the cpp logic to python too but that also didn't work, i don't know why

    • @techdose4u
      @techdose4u  18 годин тому

      I think it will work.
      Is there any implementation issue ?

    • @nileshbhanot2776
      @nileshbhanot2776 17 годин тому

      @@techdose4u
      def continuousSubarrays(self, nums: List[int]) -> int:
      l, res = 0, 0
      mini, maxi = float("inf"), float("-inf")
      for r in range(len(nums)):
      maxi = max(nums[r], maxi)
      mini = min(nums[r], mini)
      if maxi - mini > 2:
      n = r - l
      res += ((n * (n + 1)) // 2)
      l = r
      maxi = nums[r]
      mini = nums[r]
      while l > 0 and abs(nums[r] - nums[l - 1])

  • @eliomorningstar7392
    @eliomorningstar7392 День тому +1

    sir is it possible if you can explain the segment or fenwick tree approach for this problem?

    • @techdose4u
      @techdose4u  День тому +3

      that will be overkill.
      Nothing better than 2 pointer.
      currently I am focusing on easy and optimal solutions.
      Overkill is not recommended :)

  • @AKProductionsTelugu
    @AKProductionsTelugu День тому

    why don't you move the left pointer to right man its striaght forward and less complicated for explaining!

  • @firstyfirst
    @firstyfirst День тому

    would maintaining a index whenever I update max help last wala update will be stored and my next L would be ind+1;

    • @techdose4u
      @techdose4u  День тому

      The max may not be optimal.
      ex. 8,7,1
      repeatLimit: 2
      Last max will give 8 but you needed 7 :)

  • @B-Billy
    @B-Billy День тому +1

    I was with you until you start talking about moving L pointer.

    • @techdose4u
      @techdose4u  День тому

      You can rewatch and do dry run on examples to clear :)

  • @AlbinSabu-y2v
    @AlbinSabu-y2v День тому +1

    This is first time I see the window is going like a swing. That made me confusing(especially the double count & subtracton🥲). If I had never seen this algo my dumb brain could never figure out the soln.

    • @techdose4u
      @techdose4u  День тому

      This was an outlier so cant blame anyone!