Design HashSet - Leetcode 705 - Python

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

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

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

    It's ok to have collisions. There is a balance between number of collisions, number of operations and performance

  • @MP-ny3ep
    @MP-ny3ep ปีที่แล้ว +6

    Thank you for the daily leetcode problems!

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

    Where have you gone neetcode? you dont post new videos anymore and you also dont upload more content for ppl like me who paid for Pro.

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

    That's not a bug . It's a feature. It uses a reference to the pointer of ListNode(0).
    when you print(ListNode(0)). it will print the address in memory.

    • @NeetCodeIO
      @NeetCodeIO  ปีที่แล้ว +8

      Yeah, not a python bug, but a bug with the initialization that many people commonly make.

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

    7:20 AWESOME!! That was my bug and I didnt know Python does that :P Thank U

  • @YT.Nikolay
    @YT.Nikolay ปีที่แล้ว +1

    Daily problem - the best, thank you!

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

    Another way to achieve the constant O(1) time for contains() method is using boolean array with index acts as key, but the memory suffer though (constrain is 0

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

      I did that but initialized it with an array of -1s, which is [-1]*(10^6). Since the keys are positive, if the array[key] == -1, array does not contain key. To add element arr[key] = key. But I dont think this solution would fly in an interview lol

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

    why create a dummy node? why not fill values from the first node itself?

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

      That would work too, but I'm use to using dummy nodes, because it helps avoid an extra if-statement. But it's a pretty minor improvement. Maybe not worth it for this problem.

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

      @@NeetCodeIO why would we need an extra if statement?

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

      @@siddharthtanwar1529 Best way to find out is to try it yourself. But if array[key] is empty, we will have to assign it to a node. Otherwise we insert a node at the end of the list.
      A dummy node prevents the first case from ever occuring.

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

    Can we just use a two-demension array for python solution?

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

    Thank you for the video!! I thought there should be a linked list class in case of duplicate values but there is just node class. Could you explain this plz?

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

    Can you do 1206 design skip list, next? :)

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

    is it worth solving with binary search tree?

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

      I think it's will be a bonus if you solve it with bst on interview

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

    what if key == 0 ? you will create another Node with a value of 0 ?

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

    Could we use a deque instead of a custom ListNode class? :
    from collections import deque
    class MyHashSet:
    def __init__(self):
    self.num_buckets = 10000
    self.elements = [deque() for i in range(self.num_buckets)]

    def add(self, key: int) -> None:
    if not self.contains(key):
    bucket = key % self.num_buckets
    self.elements[bucket].append(key)
    def remove(self, key: int) -> None:
    if self.contains(key):
    bucket = key % self.num_buckets
    self.elements[bucket].remove(key)

    def contains(self, key: int) -> bool:
    bucket = key % self.num_buckets
    for node in self.elements[bucket]:
    if node == key:
    return True
    return False

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

    why use a linked list over a simple array for collisions, operations are still going to be O(n) with n the number of collisions, aren't they?

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

    Hello, I am following this solution word by word on LeetCode but it is saying time limit exceeded

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

      yeah I have the same issue

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

    thanks for the daily

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

    Thank you so much

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

    thanks

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

    Thumbnails are misleading xD

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

    💌💌💌

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

    thank you so much