Problem solution · Python

Design Hashmap

Design Hashmap: a Python solution using hash-based lookup. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Hash-based lookup
Source
Kamyu LeetCode Solutions
Length
94 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Hash-based lookup

For Design Hashmap, the implementation stores previously seen values or frequencies in a hash table for direct membership and lookup operations.

  1. Decide the key that represents the information needed later.
  2. Update its count or stored state while scanning the input.
  3. Use constant-time expected lookups to detect matches or assemble the result.

Code notes

  • 94 lines of Python from the credited upstream file design-hashmap.py.
  • The implementation visibly relies on hash lookup.
  • No explicit loop blocks detected.

Complexity

Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeDesign Hashmap · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(1)# Space: O(n) class ListNode(object):    def __init__(self, key, val):        self.val = val        self.key = key        self.next = None        self.prev = None  class LinkedList(object):    def __init__(self):        self.head = None        self.tail = None     def insert(self, node):        node.next, node.prev = None, None  # avoid dirty node        if self.head is None:            self.head = node        else:            self.tail.next = node            node.prev = self.tail        self.tail = node     def delete(self, node):        if node.prev:            node.prev.next = node.next        else:            self.head = node.next        if node.next:            node.next.prev = node.prev        else:            self.tail = node.prev        node.next, node.prev = None, None  # make node clean     def find(self, key):        curr = self.head        while curr:            if curr.key == key:                break            curr = curr.next        return curr  class MyHashMap(object):     def __init__(self):        """        Initialize your data structure here.        """        self.__data = [LinkedList() for _ in xrange(10000)]     def put(self, key, value):        """        value will always be positive.        :type key: int        :type value: int        :rtype: void        """        l = self.__data[key % len(self.__data)]        node = l.find(key)        if node:            node.val = value        else:            l.insert(ListNode(key, value))     def get(self, key):        """        Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key        :type key: int        :rtype: int        """        l = self.__data[key % len(self.__data)]        node = l.find(key)        if node:            return node.val        else:            return -1     def remove(self, key):        """        Removes the mapping of the specified value key if this map contains a mapping for the key        :type key: int        :rtype: void        """        l = self.__data[key % len(self.__data)]        node = l.find(key)        if node:            l.delete(node)    

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗