My Calendar II - Leetcode 731 - Python

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

КОМЕНТАРІ • 45

  • @WizoML
    @WizoML 2 місяці тому +3

    My brain rejects the possibility of an O(N^2) solution without even thinking about it. The only solution I can rationalize for this problem is the segment tree one. But looking at what you did, it seems super elegant and original.

  • @rafaellima7103
    @rafaellima7103 2 місяці тому +2

    Keep up the good work, neet!
    🎉

  • @33galactus
    @33galactus 2 місяці тому +2

    Great intuition and explanation! Just one comment - the name 'non_overlapping' for the array is little misleading as it may contain overlapping intervals as we are adding all intervals to it. I would name it as 'bookings" or 'all_intervals'.

  • @md_pedia1
    @md_pedia1 2 місяці тому +4

    Another approach:
    We could also just maintain a sortedlist(). And when booking we can add to the list the tart val and a 1 as a tuple, then we can add a tuple of the end val and -1. Then we just need to iterate over the list and add those 1s and -1s we added, if their sum becomes ==3, there is a triple booking so we remove the element and return False. 🙂

    • @33galactus
      @33galactus 2 місяці тому

      Interesting, could you share your code?

    • @md_pedia1
      @md_pedia1 2 місяці тому +3

      @@33galactus
      def __init__(self):

      self.db=SortedList()
      def book(self, start: int, end: int) -> bool:
      s,e=(start,1),(end,-1)
      self.db.add(s)
      self.db.add(e)
      total=0
      for _,c in self.db:
      total+=c
      if total==3:
      self.db.remove(s)
      self.db.remove(e)
      return False
      return True

  • @howardlam6181
    @howardlam6181 2 місяці тому

    This is O(n) per insertion. assuming insertion of n times. you have a triangle. A triangle is n*2/2 so O(n^2).

  • @jamestwosheep
    @jamestwosheep 2 місяці тому

    Damn, I was truly overthinking this one. Thank you so much for your clear and straightforward explanation!

  • @rahulsihara8946
    @rahulsihara8946 2 місяці тому +16

    no way i could think of it even after solving yesterday's problem. meh :(

    • @neks2081
      @neks2081 2 місяці тому +12

      Don’t treat it as a failure. Treat it as a learning experience! Learn, deeply understand, repeat it in your head and you will have a new tool for the future!

  • @nitinpendekanti30
    @nitinpendekanti30 2 місяці тому

    Similar to my calendar I, you could solve this with two BSTs with a time complexity of O(logn)

  • @MyLuck19
    @MyLuck19 2 місяці тому

    Use Line sweep algorithm and solve it can solve in O(n)

  • @jaatharsh
    @jaatharsh 2 місяці тому

    saviour and amazing explanation

  • @MP-ny3ep
    @MP-ny3ep 2 місяці тому

    Another great explanation. Thank you

  • @zaeempathan9983
    @zaeempathan9983 2 місяці тому +1

    love your videos buddy, thankyou for your explanation

  • @l1f3study74
    @l1f3study74 2 місяці тому

    Hi Navdeep, what's up? I was expecting the solution of today's problem 432, All O' one Data Structure, it's fine if you are not in the state of making that video, thanks for being a really good mentor ❤

  • @EduarteBDO
    @EduarteBDO 2 місяці тому

    So, if I'm understanding if we are looking for a quadruple overlapping we just add another list?

  • @rohitkumaram
    @rohitkumaram 2 місяці тому

    In that case yesterday's my cal 1 should have been this:
    ```
    class MyCalendar:
    def __init__(self):
    self.o = []
    def book(self, start: int, end: int) -> bool:
    for s, e in self.o:
    if end > s and e > start:
    return False
    self.o.append((start, end))
    return True
    ```
    Disclaimer it is accepted with 40% beating others

    • @chayceross8531
      @chayceross8531 2 місяці тому

      This is O(n) for each insertion, where as the BST from mycal 1 was O(logn) per insertion

  • @dudeyouhavenoidea
    @dudeyouhavenoidea 2 місяці тому

    Thank you

  • @pruthvinarayana9568
    @pruthvinarayana9568 2 місяці тому

    what if there were k No of bookings allowed , how would we solve it?

  • @akashverma5756
    @akashverma5756 2 місяці тому +1

    Line Sweep algorithm is easier and more intuitive.

    • @alexandrunechifor1878
      @alexandrunechifor1878 2 місяці тому

      The focus of the problem is to create a class that can handle the addition of new intervals at any point in time. The intervals are not given in a sorted list. So you will need to sort the list each time, getting a worse time complexity

    • @akashverma5756
      @akashverma5756 2 місяці тому

      @@alexandrunechifor1878 This is what treeMap(ordered set in c++) is for .

  • @fahmyboy1
    @fahmyboy1 2 місяці тому +1

    correct me if i’m wrong but can’t we just use calendar 1 solution but have two trees? you have a tree with all the double overlapped areas (not bookings, but areas), so you check every booking through that tree first. if there’s no overlap, you add to the main tree where all the bookings were stored like calendar 1. as you’re going through, you detect any overlaps. if there is a new double overlap, then you have to add that to the double overlap tree.

    • @finemechanic
      @finemechanic 2 місяці тому

      How would you build a binary tree for overlapping intervals (which interval is less if they overlap)?

  • @mohitverma-x8o9w
    @mohitverma-x8o9w 2 місяці тому

    how i'm not being able to build such logic,

  • @saone.777
    @saone.777 2 місяці тому

    Im yours big fan

  • @StellasAdi18
    @StellasAdi18 2 місяці тому +1

    This was crazy. No way one can solve in real interview without having seen.

  • @finemechanic
    @finemechanic 2 місяці тому

    Somewhat unfortunate variable naming. Apparently self.non_overlapping may contain overlapping intervals and self.overlapping cannot.

  • @lenovox1carbon664
    @lenovox1carbon664 2 місяці тому +5

    Is it not O(N^2) why is everyone saying O(N)

    • @33galactus
      @33galactus 2 місяці тому +5

      It's O(N) per input i.e. time complexity of the 'book' function. The overall time complexity for N inputs is O(N^2).

  • @VanjeAv
    @VanjeAv 2 місяці тому

    Segment tree please ❤

  • @MrACrazyHobo
    @MrACrazyHobo 2 місяці тому +1

    I'm struggling to understand how my calendar ii is O(n) time complexity, but my calendar i is O(nlogn) time complexity

    • @chayceross8531
      @chayceross8531 2 місяці тому +1

      My cal 1 was O(logn) because it used a BST

    • @ozempickin
      @ozempickin 2 місяці тому

      its O(n) per operation for this problem, sounds like you performed a sorting every operation for calendar i

    • @MrACrazyHobo
      @MrACrazyHobo 2 місяці тому

      @@ozempickin Ah it's O(n) per operation, so then it's O(n^2) total time complexity?

    • @ozempickin
      @ozempickin 2 місяці тому

      @@MrACrazyHobo yes it's a brute force O(n^2) complexity for the code given in the vid. so if u used binary search/bisect/insort to solve calendar 1 you probably have O(nlogn) (and you can appropriate it here if you wish to). but if you somehow performed sorting on every function call (which u 100% don't have to) then it would grow to be O(n^2logn).

  • @Aaron-cp9tt
    @Aaron-cp9tt 2 місяці тому

    Hi bro, get up and update today's video.🤣

    • @l1f3study74
      @l1f3study74 2 місяці тому

      lmao, here for the same thing

  • @devanshverma5395
    @devanshverma5395 2 місяці тому +3

    This is not O(N) time complexity, you're iterating over the list on every call. It's O(N^2) complexity

    • @iritesh
      @iritesh 2 місяці тому +1

      It's O(2*n)

    • @rohitkumaram
      @rohitkumaram 2 місяці тому

      same doubt...

    • @neks2081
      @neks2081 2 місяці тому

      @@iriteshit’s O(N^2) if N is the number of total calls to the function. It is O(N) if N is the number of booked events.

  • @pastori2672
    @pastori2672 2 місяці тому

    can someone tell me why this doesnt work please thank you bool:
    if not self.root:
    self.root = TreeNode([start, end])
    return True
    curr = self.root
    while curr:
    if start >= curr.range[1]:
    if not curr.right:
    curr.right = TreeNode([start, end])
    break
    curr = curr.right
    elif end l and start < r:
    return False
    res = True
    if start < curr.range[0]:
    res = res & self.book(start, curr.range[0])
    if end > curr.range[1]:
    res = res & self.book(curr.range[1], end)
    if res:
    curr.over.append([max(start, curr.range[0]), min(end, curr.range[1])])
    return res
    return True