-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathProblem2.py
More file actions
77 lines (60 loc) · 2.49 KB
/
Problem2.py
File metadata and controls
77 lines (60 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
## Problem 2: Design Hashmap (https://leetcode.com/problems/design-hashmap/)
class MyHashMap:
class Node:
def __init__(self, key: int = -1, val: int = -1, next_node: 'Node' | None = None):
self.key = key
self.val = val
self.next = next_node
# We will use chaining, so we want the link list to be small enough
# so our map will be of seze 10^4
# and link list's will have at max 10^2 nodes
def __init__(self):
self.n = 10000
self.hashMap = [self.Node() for _ in range(self.n)] # we will init the hashMap with dummy nodes
# Complexity: O(1)
def put(self, key: int, value: int) -> None:
index = self.getHashKey(key)
previous = self.findPrevious(index, key)
# previous.next will only be there when key is found in the linked list
# so we need to update the value
if previous.next:
previous.next.val = value
else: # else insert a new node
previous.next = self.Node(key, value)
# Complexity: O(1)
def get(self, key: int) -> int:
index = self.getHashKey(key)
previous = self.findPrevious(index, key)
if previous.next and previous.next.key == key:
return previous.next.val
return -1
# Complexity: O(1)
def remove(self, key: int) -> None:
index = self.getHashKey(key)
previous = self.findPrevious(index, key)
# if key, value is present in the linked list, break the link
if previous.next and previous.next.key == key:
temp = previous.next
previous.next = previous.next.next
temp.next = None
def getHashKey(self, key: int) -> int:
return key % self.n
# this is a helper function
# we need to find the previous in each operation sine we are using a linked list
# we mainly need previous for the remove operation to break the link
# but for adding/updating a node, if we know the previous, we can add to its next
# O(1) since linked list is small enough
def findPrevious(self, index: int, key: int) -> 'Node':
previous = None
current = self.hashMap[index]
while current:
if current.key == key:
break
previous = current
current = current.next
return previous
# Your MyHashMap object will be instantiated and called as such:
# obj = MyHashMap()
# obj.put(key,value)
# param_2 = obj.get(key)
# obj.remove(key)