-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathproblem-2.py
More file actions
82 lines (70 loc) · 2.32 KB
/
problem-2.py
File metadata and controls
82 lines (70 loc) · 2.32 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
78
79
80
81
82
# Design HASHMAP
# Time Complexity : put, get and remove function have O(1) time complexity and find
# function has O(N) time complexity in worst case scenario
# Space Complexity : O(M+N) where M is primary bucket and N is the bucket which has unique keys
# Did this code successfully run on Leetcode : YES
# Any problem you faced while coding this : I was missing a lot of edge cases scenarios here
# so I had some hinderance in implementingthe code all together.
class MyHashMap(object):
class Node:
def __init__(self,key,value):
self.key=key
self.value=value
self.next=None
def __init__(self):
self.primary_bucket=10000
self.size=[None]*self.primary_bucket
def hashfunc1(self,key):
return key%self.primary_bucket
def find(self,head,key):
prev=None
current=head
while current and current.key!=key:
prev=current
current=current.next
return prev
def put(self, key, value):
"""
:type key: int
:type value: int
:rtype: None
"""
primary_index=self.hashfunc1(key)
if self.size[primary_index]==None:
self.size[primary_index]=self.Node(-1,-1)
self.size[primary_index].next=self.Node(key,value)
return
prev=self.find(self.size[primary_index],key)
if prev.next is None:
prev.next=self.Node(key,value)
else:
prev.next.value=value
def get(self, key):
"""
:type key: int
:rtype: int
"""
primary_index=self.hashfunc1(key)
if self.size[primary_index] is None:
return -1
prev=self.find(self.size[primary_index],key)
if prev.next is None:
return -1
return prev.next.value
def remove(self, key):
"""
:type key: int
:rtype: None
"""
primary_index=self.hashfunc1(key)
if self.size[primary_index] is None:
return
prev=self.find(self.size[primary_index],key)
if prev.next is None:
return
prev.next=prev.next.next
# Your MyHashMap object will be instantiated and called as such:
# obj = MyHashMap()
# obj.put(key,value)
# param_2 = obj.get(key)
# obj.remove(key)