Insert Delete GetRandom O(1) - Leetcode 380 - Python

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

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

  • @z06.hassan
    @z06.hassan 2 года назад +16

    That moment when NeetCode has the video posted of the problem you're stuck on. I know you probably hear this a lot, but thank you so much. You are a lifesaver

  • @JKL95151
    @JKL95151 2 года назад +70

    Hey man, thank you so much for the videos. I was so glad to see one of the questions you covered in my interview, nailed it, then got the Amazon offer! Keep doing what you're doing!!

    • @NeetCode
      @NeetCode  2 года назад +14

      Thank you so much Marco!!

  • @julius1642
    @julius1642 2 года назад +9

    Hey neetcode, just wanna add another "thank you" message to your stack. Thank you so much and it's been helping me a lot and you are really amazing and I hope to see more in the future!

  • @bumpin7789
    @bumpin7789 2 года назад +10

    I was asked this in a Bloomberg phone interview yesterday.

  • @numberonep5404
    @numberonep5404 2 года назад +7

    More tricky than what i figured at first sight ! Thanks for the video :)

  • @shashankjoshi8250
    @shashankjoshi8250 11 месяцев назад +1

    It was really a great explanation because it is how we would actually reach the optimal answer. You backtracked the thinking of a person trying to solve this problem first time.

  • @KaranSharma-ew7io
    @KaranSharma-ew7io 2 года назад +19

    man being at google u are so much consistent , quite remarkable. can u make a video on "work pressure" at google .

  • @protomanx1
    @protomanx1 2 года назад +12

    Hey, wanted to thank you as I used your videos to practice before coding interviews and I did well and I got the job!, Thanks man! :D, I'll be sure to subscribe to your Patreon using money from my first paycheck.

    • @NeetCode
      @NeetCode  2 года назад +4

      That's awesome, congrats on the job 🎉👏

    • @NeetCode
      @NeetCode  2 года назад +1

      @@0yustas0 That's a good point, but I wonder if it would satisfy the problem's definition of "random". In this case if we call random() multiple times, will it return the same val?

    • @nikhil_a01
      @nikhil_a01 2 года назад

      @@0yustas0 pop() returns an arbitrary element which means it can return whatever the set wants. You can't rely on it to be at all random, or pseudo-random. In practice, at least for the CPython interpreter, pop() just returns the first element in the set.
      If you try to use pop() and then add() back it will just cycle through all the elements in the set in order which is not the slightest bit random, or pseudo-random.

    • @embarrassed_dodo
      @embarrassed_dodo 3 месяца назад

      @@nikhil_a01 Yeah i tried to implement this getRandom method originally using set, if we pop and item from set, store it in temp variable, and add back it again and return the temp, when called getRandom, it kind of keep cycling in a ordered fashion, it was not random

  • @albusdumbledore9311
    @albusdumbledore9311 13 дней назад +1

    Hey Neetcode, I think this is not a nice implementation as 1- the pop() and append() aren't O(1) operation, 2- it abstract away the hashing part behind the hashmap.

  • @amalant1320
    @amalant1320 10 месяцев назад +1

    Thanks bro , your are the best of best i ever scene now days i always looking for your solution because you are explaining all the optimal solution

  • @sabaamanollahi5901
    @sabaamanollahi5901 2 года назад

    Thanks NeetCode! just want to say it actually won't work when removing the last item! changing line 20 and 21 will fix it.

  • @pranavkulkarni7408
    @pranavkulkarni7408 2 года назад +3

    what an explanation man, perfect !!! 🙌🙌🙌

  • @hackysack1988
    @hackysack1988 2 года назад +4

    Hey neet, great video as always! I watched your video on finally being employed the other day and I was curious about if you still plan on creating some videos on system design? I think you're one of the best educators on this platform and I would really love to see your take on the topic :)

  • @pratikmhatre4815
    @pratikmhatre4815 Год назад

    Feels like you were not feeling well while making this video as I am following yor videos for quite long time now.
    We appreciate your efforts bro 🙂

  • @amoghyelasangikar5836
    @amoghyelasangikar5836 2 года назад +4

    Hi...please make a video on Logic building and how to increase problem solving skill for beginners...! Using python.

  • @AseshShrestha
    @AseshShrestha 2 года назад +1

    Congrats on 100k subscribers 🎉👏

  • @kotachaitanya237
    @kotachaitanya237 3 месяца назад

    hey @NeetCode, I remember this question, the first time I saw the question is you asked in the mock interview to that meta intern youtuber I don't remember his name and I am sure he was rejected at the end of the interview.

  • @kamalasowmya7120
    @kamalasowmya7120 11 месяцев назад

    thank you for a detailed and easy explanation buddy!

  • @Dharm1k987
    @Dharm1k987 2 года назад +2

    hey neetcode curious what you use to record your whiteboard. is it a desktop software or you do it on ipad

    • @NeetCode
      @NeetCode  2 года назад +3

      I just use Paint3D (free) and a mouse with low dpi

  • @Ashasri-q6v
    @Ashasri-q6v 3 месяца назад +1

    How to solve this problem on a locally installed python and run the test cases manually??

  • @nikhil199029
    @nikhil199029 2 года назад

    Use linklist instead of array.
    Store the pointer to node instead of index in hashmap

    • @dragonb3ar
      @dragonb3ar 2 года назад +5

      You need O(1) index for get random which you don’t have with a linked list

    • @correabuscar
      @correabuscar 2 года назад +1

      but that might run into memory fragmentation issues? sounds good otherwise, I knew there must be a better way. Thanks! Ah, crap, Andrew is is right, you need index for getrandom, dang it

  • @AbanoubAsaad-YT
    @AbanoubAsaad-YT 2 года назад

    That's awesome man, thanks a lot :)

  • @sugarjay.cherry7823
    @sugarjay.cherry7823 2 года назад +1

    I think it should be len(self.numList)-1 shouldn't it? Or you'll constantly have the wrong index

    • @iamsmitthakkar
      @iamsmitthakkar Год назад

      Not really because we are adding the element in the next line. If we were to add the element in the list before we add it to hashmap, then we would need to do -1 but not in this case

  • @mdilyaspasha2736
    @mdilyaspasha2736 2 года назад

    Hello sir ur videos are great,amazing explanation,please make it in java language

  • @AneeshMelkot
    @AneeshMelkot 2 года назад

    I used a Doubly Linked List to store the vals and operate in O(1) time. Is that overcomplicating things?

    • @chudchadanstud
      @chudchadanstud 9 месяцев назад

      Traversing a linked list is O(n). The getRandom method is no longer O(1).
      If you use a array removing anything that is not at the end (or front, depending on how you store data in the array) is also O(n).

  • @sarojpaudel7479
    @sarojpaudel7479 9 месяцев назад

    every crud operation is O(1) in hashmap. so why we cant use hashmap in this question

  • @bohanxu422
    @bohanxu422 2 года назад

    Can't we just construct a basic hash set our selves? like using linear probing so we actually have a build-in index for elements.

  • @_7__716
    @_7__716 2 года назад

    In numMap are the values mappedd as.{val:index} or {index:val} ?

    • @nikhil_a01
      @nikhil_a01 2 года назад

      The numMap is val: index. The numList is what gives you index: val.

  • @ohyash
    @ohyash 2 года назад +1

    I was given a variation of this problem in an interview last week.

    • @ohyash
      @ohyash 2 года назад +1

      For anyone interested in how'd it go for a normal-ish interviewee:
      I had similar solution of keeping a set(good for search) AND a vector(good for random access) and was struggling to optimise the remove (`find` operation in my case).
      The interviewer gave a hint "can you try to combine the two data structures?"
      And it was a good hint because combining means making links to refer any vector index from the set, and I was able to think of Map in a lil bit.

    • @mehershrishtinigam5449
      @mehershrishtinigam5449 2 года назад

      @@ohyash Thanks for sharing

    • @iamsupriyo
      @iamsupriyo Год назад

      Hm, thanks man@@ohyash

  • @thetryitproject
    @thetryitproject 2 года назад

    Please do system design videos

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

    instead of creating a separate list and updating elements in it based on hashmap values, we can just use a hashset and the get random value as ` return random.choice(list(self.numSet)) `

  • @hr3392
    @hr3392 2 года назад

    Doesn't using len making it O(n) though?

    • @hr3392
      @hr3392 2 года назад

      nvm just found out len is O(1) in python

  • @rayhaanhanslod4792
    @rayhaanhanslod4792 2 года назад

    Why not use a linked list?

    • @johnleon4065
      @johnleon4065 2 года назад +1

      Searching to check if the element exist in a linked list is O(n) in a linked list making it violate our condition of it being O(1)

  • @masternobody1896
    @masternobody1896 2 года назад +2

    First

  • @gregoryoliveira8358
    @gregoryoliveira8358 2 года назад

    Hi. I use the build set type.
    class RandomizedSet:
    def __init__(self):
    self.setNum = set([])
    def insert(self, val: int) -> bool:
    isPresent = val not in self.setNum
    self.setNum.add(val)
    return isPresent
    def remove(self, val: int) -> bool:
    isPresent = val in self.setNum
    self.setNum.discard(val)
    return isPresent
    def getRandom(self) -> int:
    return random.choice(list(self.setNum))

    # Your RandomizedSet object will be instantiated and called as such:
    # obj = RandomizedSet()
    # param_1 = obj.insert(val)
    # param_2 = obj.remove(val)
    # param_3 = obj.getRandom()
    The memory used is equal of your solution. However, my runtime if almost 3x your solution. I guess because I needed to convert the set into a list to use the random. What do you think?

    • @nikhil_a01
      @nikhil_a01 2 года назад +2

      Yes, converting the set into a list is O(N) time complexity because it has to go through all the elements in the set. So your getRandom() is O(N) time complexity, not O(1).
      Since the problem explicitly asks for an O(1) average time complexity for getRandom(), the solution you posted doesn't fulfill the criteria.