Design HashSet - Leetcode 705 - Python

preview_player
Показать описание
Solving Leetcode 705 - Design hashset, today's daily leetcode on may 29.

0:00 - Read the problem
1:25 - Drawing Explanation
6:35 - Coding Explanation

leetcode 705

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

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.

metarus
Автор

Thank you for the daily leetcode problems!

MP-nyep
Автор

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.

redniq
Автор

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

siddharthtanwar
Автор

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

MansoorLunawadi
Автор

is it worth solving with binary search tree?

lingyuhu
Автор

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


def remove(self, key: int) -> None:
if self.contains(key):
bucket = key % self.num_buckets



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

servantofthelord
Автор

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

madhuvamsimachavarapu
Автор

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

michaelyao
Автор

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?

memeproductions
Автор

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?

thegreatestcomesfromthewor
Автор

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

flatmapper
Автор

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 <= key <= 10^6)

vietnguyenquoc
Автор

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

medam
Автор

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

TheElementFive