-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign_hashmap.py
333 lines (281 loc) · 11.3 KB
/
design_hashmap.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env python3
# Design HashMap
#
# https://leetcode.com/problems/design-hashmap
#
# Design a HashMap without using any built-in hash table libraries.
# Implement the MyHashMap class:
#
# MyHashMap() initializes the object with an empty map.
# void put(int key, int value) inserts a (key, value) pair into the HashMap. If
# the key already exists in the map, update the corresponding value.
# int get(int key) returns the value to which the specified key is mapped, or -1
# if this map contains no mapping for the key.
# void remove(key) removes the key and its corresponding value if the map
# contains the mapping for the key.
from itertools import chain
from typing import Optional, Tuple
def test():
"""
Run `pytest <this-file>`.
"""
def test_algo(algo):
hashmap = algo()
hashmap.put(1, 1)
# The map is now [[1,1]]
hashmap.put(2, 2)
# The map is now [[1,1], [2,2]]
assert hashmap.get(1) == 1
# return 1, The map is now [[1,1], [2,2]]
assert hashmap.get(3) == -1
# return -1 (i.e., not found), The map is now [[1,1], [2,2]]
hashmap.put(2, 1)
# The map is now [[1,1], [2,1]] (i.e., update the existing value)
assert hashmap.get(2) == 1
# return 1, The map is now [[1,1], [2,1]]
hashmap.remove(2)
# remove the mapping for 2, The map is now [[1,1]]
assert hashmap.get(2) == -1
# return -1 (i.e., not found), The map is now [[1,1]]
# Test all different algorithms/implementations
solution = Solution()
for algo in [
solution.list_of_tuples,
solution.table,
solution.hashing_by_chaining,
solution.hashing_by_linear_probing,
solution.double_hashing,
]:
test_algo(algo)
class Solution:
def list_of_tuples(self):
"""
Approach: Brute-force.
Idea: ?
Time: O(?): ?
Space: O(?): ?
Leetcode: 1921 ms runtime, 19.78 MB memory
"""
class MyHashMap:
def __init__(self):
# Store a list of (key, value) tuples.
self.l = []
def find_key_idx(self, key: int) -> Optional[int]:
# O(n)
# Search for existing key.
for i, (k, _v) in enumerate(self.l):
if key == k:
return i
return None
def put(self, key: int, value: int) -> None:
# O(n)
if (i := self.find_key_idx(key)) is not None:
# Update value of existing key.
self.l[i] = (key, value)
else:
# Insert new key, value pair.
self.l.append((key, value))
def get(self, key: int) -> int:
# O(n)
if (i := self.find_key_idx(key)) is not None:
(_k, v) = self.l[i]
return v
else:
return -1
def remove(self, key: int) -> None:
# O(n)
if (i := self.find_key_idx(key)) is not None:
# O(1)
# Swap remove.
self.l[i], self.l[-1] = self.l[-1], self.l[i]
self.l.pop()
return MyHashMap()
def table(self):
"""
Approach: Brute-force.
Idea: ?
Time: O(?): ?
Space: O(?): ?
Leetcode: 764 ms runtime, 42.42 MB memory
"""
class MyHashMap:
def __init__(self):
# The key is the index into a giant fixed-size table with space
# for every key.
MAX_KEY = 10**6 + 1
self.table = [None for _ in range(MAX_KEY)]
def put(self, key: int, value: int) -> None:
# O(1)
self.table[key] = value
def get(self, key: int) -> int:
# O(1)
if self.table[key] is not None:
return self.table[key]
else:
return -1
def remove(self, key: int) -> None:
# O(1)
self.table[key] = None
return MyHashMap()
def hashing_by_chaining(self):
"""
Approach: Brute-force.
Idea: ?
Time: O(?): ?
Space: O(?): ?
Leetcode: 186 ms runtime, 19.96 MB memory
"""
class MyHashMap:
def __init__(self):
# The key is the index pointing to a bucket in a fixed-sized hashtable.
self.size = 2048
self.table = [[] for _ in range(self.size)]
def hash(self, x: int) -> int:
return x % self.size
def find_key_idx(self, key: int) -> Tuple[int, Optional[int]]:
# Average O(1)
# Search for existing key.
h = self.hash(key)
for i, (k, _v) in enumerate(self.table[h]):
if key == k:
return (h, i)
return (h, None)
def put(self, key: int, value: int) -> None:
# Average O(1)
(h, i_opt) = self.find_key_idx(key)
if (i := i_opt) is not None:
# Update value of existing key.
self.table[h][i] = (key, value)
else:
# Insert new key, value pair.
self.table[h].append((key, value))
def get(self, key: int) -> int:
# Average O(1)
(h, i_opt) = self.find_key_idx(key)
if (i := i_opt) is not None:
(_k, v) = self.table[h][i]
return v
else:
return -1
def remove(self, key: int) -> None:
# Average O(1)
(h, i_opt) = self.find_key_idx(key)
if (i := i_opt) is not None:
# O(1)
# Swap remove.
self.table[h][i], self.table[h][-1] = (
self.table[h][-1],
self.table[h][i],
)
self.table[h].pop()
return MyHashMap()
def hashing_by_linear_probing(self):
"""
Approach: Brute-force.
Idea: ?
Time: O(?): ?
Space: O(?): ?
Leetcode: 1147 ms runtime, 19.97 MB memory
"""
class MyHashMap:
def __init__(self):
# The key is the index pointing to a bucket in a fixed-sized hashtable.
MAX_SIZE = 10**4 + 1
self.size = MAX_SIZE
self.table = [None for _ in range(self.size)]
def hash(self, x: int) -> int:
return x % self.size
def find_key_idx(self, key: int) -> Tuple[bool, int]:
# Average O(1)
# Search for existing key, and return (true, i) if the k,v pair
# exists at idx i, or (false, i) where i is the idx at which
# the k,v would be inserted.
h = self.hash(key)
# If (k,v) is not stored at table[h], it will be stored at some
# table[h+k] (including wrap around). Check them one by one.
# We are guaranteed to find a free spot.
for i in chain(range(h, self.size), range(0, h)):
if (kv := self.table[i]) is not None:
(k, _v) = kv
if key == k:
return (True, i)
else:
return (False, i)
raise Exception("there must be a free space for the new key")
def put(self, key: int, value: int) -> None:
# Average O(1)
(_key_exists, i) = self.find_key_idx(key)
self.table[i] = (key, value)
def get(self, key: int) -> int:
# Average O(1)
(key_exists, i) = self.find_key_idx(key)
if key_exists:
(_k, v) = self.table[i]
return v
else:
return -1
def remove(self, key: int) -> None:
# Average O(1)
(_key_exists, i) = self.find_key_idx(key)
# NOTE: We're not allowed to "free" this slot (e.g. reset it to
# None), because that would cause future probing walks to fail
# to find its correct hashed key (we would find a free slot
# before the actual hashed key). We call this slot a "tombstone".
self.table[i] = (-1, -1)
return MyHashMap()
def double_hashing(self):
"""
Approach: Brute-force.
Idea: ?
Time: O(?): ?
Space: O(?): ?
Leetcode: 3865 ms runtime, 20.08 MB memory
"""
class MyHashMap:
def __init__(self):
# The key is the index pointing to a bucket in a fixed-sized hashtable.
MAX_SIZE = 10**4 + 1
self.size = MAX_SIZE
self.table = [None for _ in range(self.size)]
def h1(self, x: int) -> int:
return x % self.size
def h2(self, x: int) -> int:
return 2 * x + 1
def find_key_idx(self, key: int) -> Tuple[bool, int]:
# Average O(1)
# Search for existing key, and return (true, i) if the k,v pair
# exists at idx i, or (false, i) where i is the idx at which
# the k,v would be inserted.
# If (k,v) is not stored at table[(h1 + j*h2)%n], continue
# searching at table[(h1 + (j+1)*h2)%n]. Check them one by one.
# We are guaranteed to find a free spot.
for j in range(0, self.size):
i = (self.h1(key) + j * self.h2(key)) % self.size
if (kv := self.table[i]) is not None:
(k, _v) = kv
if key == k:
return (True, i)
else:
return (False, i)
raise Exception("there must be a free space for the new key")
def put(self, key: int, value: int) -> None:
# Average O(1)
(_key_exists, i) = self.find_key_idx(key)
self.table[i] = (key, value)
def get(self, key: int) -> int:
# Average O(1)
(key_exists, i) = self.find_key_idx(key)
if key_exists:
(_k, v) = self.table[i]
return v
else:
return -1
def remove(self, key: int) -> None:
# Average O(1)
(_key_exists, i) = self.find_key_idx(key)
# NOTE: We're not allowed to "free" this slot (e.g. reset it to
# None), because that would cause future probing walks to fail
# to find its correct hashed key (we would find a free slot
# before the actual hashed key). We call this slot a "tombstone".
self.table[i] = (-1, -1)
return MyHashMap()