0

learning here!

I've managed to pull together this solution for a question on Leetcode! Heres the stats for this solution: Runtime: 1236 ms, faster than 17.28% of Python3 online submissions for Design HashSet Memory Usage: 18.7 MB, less than 83.53% of Python3 online submissions for Design HashSet.

Now - I wish that Leetcode would show an Ideal or Best practice solution just so I can compare and learn with mine! But they don't!

So, It would be appreciated if any of you can here! (Critique and show your Best practice solution)

Task Description

My Solution

class MyHashSet:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.TheSet = []
        

    def add(self, key: int) -> None:
        if self.contains(key):
            pass
        else:
            if key >= 0 and key <= 10**6:
                self.TheSet.append(key)
            else:
                pass

    def remove(self, key: int) -> None:
        if self.contains(key):
            self.TheSet.remove(key)
        else:
            pass

    def contains(self, key: int) -> bool:
        if key in self.TheSet:
            return True
        else:
            return False
        


# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)```

  • Would you mind sharing the link to the challenge? Your code does not use Hashes as the name would suggest. Perhaps, that could improve your solution. – JANO Jun 27 '21 at 22:29
  • 1
    Please edit the images in your question so they are __text__ so code is a [mre] including data __as text__ – DisappointedByUnaccountableMod Jun 27 '21 at 22:58
  • 1
    "Here is my code, it works but maybe it can be improved, please critique" isn't a question suitable for Stack Overflow - this should be asked at http://codereview.stackexchange.com/ – kaya3 Jun 27 '21 at 23:33
  • Because you aren't actually implementing a hash set... You are just using a list... The whole point of hash sets are hat they are better performing than lists for checking membership – juanpa.arrivillaga Jun 27 '21 at 23:33

1 Answers1

1

I found the challenge and played a little bit. I guess the problem is that you do not use the underlying concept of a HashSet, which would be explained in the challenge you tried on LeetCode. Here is also a question where they explained what a HashMap does. Would check it out!

I also made a solution for you based on this answer. It has a runtime of better than 80% and memory usage better than 90%. Clearly, this can be further improved, but I think it contains the essential concepts.

class MyHashSet:

    def __init__(self):
        self.contents = [None] * 1_000
        
    def hash(self, x):
        return x % (2 ** 61 - 1)
    
    def add(self, key:int) -> None:
        key_hash = self.hash(key) % 1_000
        bucket = self.contents[key_hash]
        
        if bucket is None:
            self.contents[key_hash] = [key]
        elif key not in bucket:
            bucket.append(key)
        return None

    def remove(self, key: int) -> None:
        key_hash = self.hash(key) % 1_000
        bucket = self.contents[key_hash]
        
        if bucket is not None:
            if key in bucket:
                bucket.remove(key)
        else:
            pass
        
    def contains(self, key: int) -> bool:
        key_hash = self.hash(key) % 1_000
        bucket = self.contents[key_hash]
        
        if bucket is not None:
            if key in bucket:
                return True
            else:
                return False
        else:
            return False

JANO
  • 2,995
  • 2
  • 14
  • 29