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.
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
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
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 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.
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?
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
It's ok to have collisions. There is a balance between number of collisions, number of operations and performance
Thank you for the daily leetcode problems!
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.
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.
Yeah, not a python bug, but a bug with the initialization that many people commonly make.
7:20 AWESOME!! That was my bug and I didnt know Python does that :P Thank U
Daily problem - the best, thank you!
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
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
why create a dummy node? why not fill values from the first node itself?
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.
@@NeetCodeIO why would we need an extra if statement?
@@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.
Can we just use a two-demension array for python solution?
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?
Can you do 1206 design skip list, next? :)
is it worth solving with binary search tree?
I think it's will be a bonus if you solve it with bst on interview
what if key == 0 ? you will create another Node with a value of 0 ?
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
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?
Hello, I am following this solution word by word on LeetCode but it is saying time limit exceeded
yeah I have the same issue
thanks for the daily
Thank you so much
thanks
Thumbnails are misleading xD
How come?
💌💌💌
thank you so much