Minimum Interval to Include Each Query - Leetcode 1851 - Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 29 พ.ย. 2024

ความคิดเห็น • 57

  • @JRK_RIDES
    @JRK_RIDES ปีที่แล้ว +36

    Buddy you don't know how much I appreciate you saying that this problem is hard again and again. This is because a lot of times when I'm not able to solve a problem it's extremely demotivating. However, knowing that others are struggling to solve this problem as well gives me some hope.

    • @nehaa3778
      @nehaa3778 ปีที่แล้ว

      ++1

    • @nikhil199029
      @nikhil199029 7 หลายเดือนก่อน

      +=1

    • @abhimanyuambastha2595
      @abhimanyuambastha2595 5 หลายเดือนก่อน

      For people struggling with these types of problems, know that you should look up questions belonging to a particular pattern. Like this one can be put into line sweep algorithms or merge interval type questions. Once you get the intuition on how to deal with these types of problems, it becomes easy even with hard problems. For example I solved this question myself before coming to neetcode and checking out the video. I was able to solve it because I've solved similar questions, so # 2252 - Number of flowers in full bloom and # 1353 - Maximum Number of Events that can be attended. These problems are very similar with a little change in implementation. Also these types of problems can be solved using multiple methods. Heap/PQ, Using prefix sum with line sweep algo, or using 2 arrays with heap or two pointers. It is worth it to look up different solutions and invest time in understanding how each one works. Next time when you see a similar question, you will have all the tools needed to solve it

    • @m_jdm357
      @m_jdm357 5 หลายเดือนก่อน +2

      You mean that he doesn't pretend that the problem is easy and brag to everyone that the problem is easy and he was able to solve it. I can't solve this, this is ridiculous. When I saw the solution I thought what is this s...

    • @martinpodhaisky6451
      @martinpodhaisky6451 3 หลายเดือนก่อน

      A lot of these query problems can be cheesed using segment trees and there are a lot of other leetcode Problems like this. I actually found a brute force solution that got accepted by compressing the coordinates and jumping over seen spaces 😂 no heap needed

  • @srikanthrao991
    @srikanthrao991 2 ปีที่แล้ว +19

    i love you bro, you're a legend. Going through your 150 list.

    • @coding_ss632
      @coding_ss632 7 หลายเดือนก่อน +2

      Were you able to get into FAANG?

  • @NikhilSharma-xv6gx
    @NikhilSharma-xv6gx 3 ปีที่แล้ว +15

    Thanks a lot. I've been stuck with tutorials and couldn't find a good enough explanation. But you explained this question perfectly. 😇

    • @NeetCode
      @NeetCode  3 ปีที่แล้ว +1

      Thanks, happy to help 🐻

  • @odysy5179
    @odysy5179 5 หลายเดือนก่อน +4

    The way I handled the result indices was at the start, turning queries into a list of tuples like (query value i, i). That way we can still sort by value, but also access the correct index directly in our main loop and therefore keep result as a list. Its the same time and space the hashmap, but I think it might be a little cleaner.

  • @lightyagami5755
    @lightyagami5755 2 ปีที่แล้ว +9

    Great explanation bro 🔥🔥 you made a hard problem seem very easy, was stuck on it for 1 hr. coded in 5 mins after understanding the intuition

  • @chetansahu1505
    @chetansahu1505 3 ปีที่แล้ว +5

    This guy is a gem. Awesome

  • @lucas29476
    @lucas29476 ปีที่แล้ว +3

    This solution is brilliant because it LAZILY pops unwanted entries from the minHeap. For example, at 16:13, if there were a (100, 4) entry in the minHeap, you wouldn't care about it, because it wouldn't matter, as it wouldn't affect the result for query "5", which would be (4, 6).

    • @raghavsrinivasan7746
      @raghavsrinivasan7746 ปีที่แล้ว

      Yes! I think this might have been worth going over in detail in the video, since it was hard to wrap my head around. Since we only care about the smallest length anyway, we can leave previous intervals in our minheap that are not the smallest. Otherwise, we can't use heappop at all - we would have to search for every interval to remove manually. This is what I was really stuck on

  • @DeepSingh-zs2oi
    @DeepSingh-zs2oi ปีที่แล้ว +1

    Thanks a lot Neetcode!! These videos make life so much easy.

  • @dheepthaaanand3214
    @dheepthaaanand3214 หลายเดือนก่อน

    What's implicit is that we don't need to check the left value before popping because such an interval would have never made it to the heap if left value was greater than query - because we're sorting the queries. For example, for query 5, if there was a (1,7) in the interval, it would be wrong because 5 isnt in range [6,7], but (1,7) could have never made it to the heap for elements less than 5

  • @АльбусДамблодр
    @АльбусДамблодр 5 หลายเดือนก่อน +1

    bro i love your explanations, thanks a lot from Russia!

  • @christendombaffler
    @christendombaffler ปีที่แล้ว +1

    It's not often you stumble upon a problem that's as beautiful as it is tough. The only thing I'm sad about is that you need to pop from the min heap after all, because a pure O(n logn) solution would've been the epitome of elegance.

  • @akhma102
    @akhma102 2 หลายเดือนก่อน

    Thanks, Neet! Interesting problem. Well explained!

  • @IAmIndraTheGod
    @IAmIndraTheGod ปีที่แล้ว

    Thanks Buddy, was able to submit my solution after getting the Explanation

  • @kapilrules
    @kapilrules 3 หลายเดือนก่อน +2

    If at all I get into META it will be only because of you . I have interview lined up. I will make sure to come back and reply here if I make it through

  • @MohSo14
    @MohSo14 หลายเดือนก่อน

    Thank you for your great explanation...!

  • @cdrrjt
    @cdrrjt 4 ชั่วโมงที่ผ่านมา

    i solved this question myselff : ))))))) i m soo happyyyyyyyyy

  • @corgirun7892
    @corgirun7892 2 ปีที่แล้ว

    amazing video. this solution beat 100% in Python3

  • @bchen1403
    @bchen1403 2 ปีที่แล้ว +11

    this is an atrocious question!!

    • @NeetCode
      @NeetCode  2 ปีที่แล้ว +4

      Completely agree

    • @GFC1337
      @GFC1337 2 ปีที่แล้ว

      @@NeetCode I really enjoyed this question actually lol. I came up with basically the same solution but had to watch the video to see if you were about to teach us how to use segment trees lmao.

  • @sabaamanollahi5901
    @sabaamanollahi5901 2 ปีที่แล้ว

    Thank You, It helped a lot!

  • @devonchia5976
    @devonchia5976 3 หลายเดือนก่อน

    why time complexity of heappop() is not added into the overall complexity?

  • @technophile_
    @technophile_ 3 หลายเดือนก่อน

    This is an insane problem 😮‍💨

  • @dabaaku
    @dabaaku 3 ปีที่แล้ว

    great video, for time complexity, the loop over Q sorted queries has a heap push and pop operations (logN), wouldn't time complexity be Q*logQ*logN ?

    • @xxRAP13Rxx
      @xxRAP13Rxx 10 หลายเดือนก่อน +2

      Here's my explanation of NeetCode's time complexity:
      Time = O(NlogN + QlogQ + Q + NlogN + NlogN) = O(NlogN + QlogQ) because we must sort both intervals and queries (i.e. O(NlogN + QlogQ) ), iterate thru the sorted queries (i.e. O(Q)), pop

  • @Asdelatech
    @Asdelatech 4 หลายเดือนก่อน

    thank you so much man

  • @asdfasyakitori8514
    @asdfasyakitori8514 ปีที่แล้ว

    Great video!

  • @eamonmahon6622
    @eamonmahon6622 2 ปีที่แล้ว

    Great video! Why didn't you have to use heapify?

    • @aliceyhu
      @aliceyhu ปีที่แล้ว +2

      We're not using heapify bc we're starting with an empty array (no values). heapify is only useful if we want to initialize a heap with several values.

    • @danielsun716
      @danielsun716 ปีที่แล้ว

      and another thing is there are two ways to create a minHeap in Python: heapq.heappush() and heapq.heapify(). The difference btw them is heappush gonna maintain the existed data in an array, like [4,7,5] then heappush(0), it will be [ 0,4,7,5]. last but not least, it will be [0,4,5,7] if we heapify().

  • @jonaskhanwald566
    @jonaskhanwald566 3 ปีที่แล้ว

    1851 the very recent problem of leetcode. Thanks. Dont know which time u r posting videos. In India, its 10 pm.

  • @jonaskhanwald566
    @jonaskhanwald566 3 ปีที่แล้ว

    do a video on sudoku using backtracking

  • @raviprakash4-yearb.tech.ch375
    @raviprakash4-yearb.tech.ch375 2 ปีที่แล้ว

    thanks a lot

  • @houmankamali5617
    @houmankamali5617 2 ปีที่แล้ว

    Thanks, another great tutorial! Question about the time complexity, wouldn't adding a new entry to the heap be an O(n) operation and so if we start with a heap of size 1 and end of up with a final size of n we will have an O(n^2) total time complexity in the worst case if we have to keep all the intervals in the heap? If so, wouldn't a sorted map structure e.g. with a balanced tree instead of a heap have a better time complexity e.g. similar to the idea used in solving skyline problem?

    • @WeirdAlSuperFan
      @WeirdAlSuperFan 2 ปีที่แล้ว +3

      adding a single element to an existing heap is O(logn)

  • @reggiehurley1479
    @reggiehurley1479 9 หลายเดือนก่อน

    does tiebreaker matter for this solution to work?

    • @tianzejiang7669
      @tianzejiang7669 9 หลายเดือนก่อน

      not really, the second value in minHeap is mainly to remove invalid intervals

  • @_7__716
    @_7__716 2 ปีที่แล้ว

    Maybe I missed it but what if two intervals start at the same point but have diff lengths? How would you be sure the shorter one is at the top of the heap?

    • @mygosity
      @mygosity 2 ปีที่แล้ว +5

      The min heap is sorted by interval length so the shortest will always appear at the front of the heap.

  • @wat5931
    @wat5931 ปีที่แล้ว

    With this code, the heap keeps stale things that have a large value, popping first should reduce the memory used

  • @vishwasnegi5184
    @vishwasnegi5184 2 ปีที่แล้ว

    6:02 it's value is 2 so what are we gonna do 😂😂

  • @dailycitycommute7440
    @dailycitycommute7440 หลายเดือนก่อน

    messy