-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path02_HashMapDesigns.cpp
More file actions
383 lines (306 loc) · 11.9 KB
/
Copy path02_HashMapDesigns.cpp
File metadata and controls
383 lines (306 loc) · 11.9 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
================================================================================
DESIGN - HASHMAP & RANDOMIZED STRUCTURES
================================================================================
Key patterns:
- HashMap implementation: Array of buckets with chaining
- Random O(1): Array + HashMap (swap with last on delete)
================================================================================
*/
#include <bits/stdc++.h>
using namespace std;
/*
PROBLEM 1: Design HashMap (LeetCode 706)
────────────────────────────────────────
Implement HashMap without built-in hash table libraries.
Design: Array of buckets, each bucket is a list for collision handling.
Time: O(n/k) average, O(n) worst | Space: O(k + n)
*/
class MyHashMap {
static const int SIZE = 10007; // Prime number for better distribution
vector<list<pair<int, int>>> buckets;
int hash(int key) {
return key % SIZE;
}
public:
MyHashMap() : buckets(SIZE) {}
void put(int key, int value) {
int idx = hash(key);
for (auto& [k, v] : buckets[idx]) {
if (k == key) {
v = value;
return;
}
}
buckets[idx].push_back({key, value});
}
int get(int key) {
int idx = hash(key);
for (auto& [k, v] : buckets[idx]) {
if (k == key) return v;
}
return -1;
}
void remove(int key) {
int idx = hash(key);
buckets[idx].remove_if([key](auto& p) { return p.first == key; });
}
};
/*
PROBLEM 2: Design HashSet (LeetCode 705)
────────────────────────────────────────
Implement HashSet without built-in hash set libraries.
Time: O(n/k) average | Space: O(k + n)
*/
class MyHashSet {
static const int SIZE = 10007;
vector<list<int>> buckets;
public:
MyHashSet() : buckets(SIZE) {}
void add(int key) {
int idx = key % SIZE;
for (int k : buckets[idx]) {
if (k == key) return;
}
buckets[idx].push_back(key);
}
void remove(int key) {
int idx = key % SIZE;
buckets[idx].remove(key);
}
bool contains(int key) {
int idx = key % SIZE;
for (int k : buckets[idx]) {
if (k == key) return true;
}
return false;
}
};
/*
PROBLEM 3: Insert Delete GetRandom O(1) (LeetCode 380) ⭐ GOOGLE FAVORITE
─────────────────────────────────────────────────────────────────────────
Support insert, remove, getRandom all in O(1).
Design:
- Array to store elements (for random access)
- HashMap: value → index in array
- On delete: swap with last element, then pop
Time: O(1) for all operations | Space: O(n)
*/
class RandomizedSet {
vector<int> nums;
unordered_map<int, int> valToIdx;
public:
bool insert(int val) {
if (valToIdx.count(val)) return false;
valToIdx[val] = nums.size();
nums.push_back(val);
return true;
}
bool remove(int val) {
if (!valToIdx.count(val)) return false;
int idx = valToIdx[val];
int lastVal = nums.back();
// Swap with last
nums[idx] = lastVal;
valToIdx[lastVal] = idx;
// Remove last
nums.pop_back();
valToIdx.erase(val);
return true;
}
int getRandom() {
return nums[rand() % nums.size()];
}
};
/*
PROBLEM 4: Insert Delete GetRandom O(1) - Duplicates Allowed (LeetCode 381)
───────────────────────────────────────────────────────────────────────────
Same as above but allow duplicates.
Design: HashMap: value → set of indices
Time: O(1) average | Space: O(n)
*/
class RandomizedCollection {
vector<int> nums;
unordered_map<int, unordered_set<int>> valToIndices;
public:
bool insert(int val) {
bool notPresent = valToIndices[val].empty();
valToIndices[val].insert(nums.size());
nums.push_back(val);
return notPresent;
}
bool remove(int val) {
if (valToIndices[val].empty()) return false;
int idx = *valToIndices[val].begin();
valToIndices[val].erase(idx);
int lastVal = nums.back();
if (idx != nums.size() - 1) {
nums[idx] = lastVal;
valToIndices[lastVal].erase(nums.size() - 1);
valToIndices[lastVal].insert(idx);
}
nums.pop_back();
return true;
}
int getRandom() {
return nums[rand() % nums.size()];
}
};
/*
PROBLEM 5: Design Underground System (LeetCode 1396)
────────────────────────────────────────────────────
Track passenger journeys and calculate average travel times.
Time: O(1) for all operations | Space: O(n)
*/
class UndergroundSystem {
unordered_map<int, pair<string, int>> checkIns; // id → {station, time}
unordered_map<string, pair<long long, int>> journeys; // route → {totalTime, count}
string getRoute(const string& start, const string& end) {
return start + "->" + end;
}
public:
void checkIn(int id, string stationName, int t) {
checkIns[id] = {stationName, t};
}
void checkOut(int id, string stationName, int t) {
auto& [startStation, startTime] = checkIns[id];
string route = getRoute(startStation, stationName);
journeys[route].first += t - startTime;
journeys[route].second++;
checkIns.erase(id);
}
double getAverageTime(string startStation, string endStation) {
string route = getRoute(startStation, endStation);
auto& [totalTime, count] = journeys[route];
return (double)totalTime / count;
}
};
/*
PROBLEM 6: Design a Number Container System (LeetCode 2349)
───────────────────────────────────────────────────────────
change(index, number): Set container at index to number.
find(number): Return smallest index with this number.
Time: O(log n) | Space: O(n)
*/
class NumberContainers {
unordered_map<int, int> indexToNum;
unordered_map<int, set<int>> numToIndices;
public:
void change(int index, int number) {
if (indexToNum.count(index)) {
int oldNum = indexToNum[index];
numToIndices[oldNum].erase(index);
if (numToIndices[oldNum].empty()) {
numToIndices.erase(oldNum);
}
}
indexToNum[index] = number;
numToIndices[number].insert(index);
}
int find(int number) {
if (!numToIndices.count(number) || numToIndices[number].empty()) {
return -1;
}
return *numToIndices[number].begin();
}
};
/*
PROBLEM 7: First Unique Number (LeetCode 1429)
──────────────────────────────────────────────
Queue-like structure that returns first unique number.
Design: LinkedHashSet-like structure (maintains insertion order)
*/
class FirstUnique {
list<int> uniqueList;
unordered_map<int, list<int>::iterator> numToIter;
unordered_set<int> duplicates;
public:
FirstUnique(vector<int>& nums) {
for (int num : nums) add(num);
}
int showFirstUnique() {
return uniqueList.empty() ? -1 : uniqueList.front();
}
void add(int value) {
if (duplicates.count(value)) return;
if (numToIter.count(value)) {
uniqueList.erase(numToIter[value]);
numToIter.erase(value);
duplicates.insert(value);
} else {
uniqueList.push_back(value);
numToIter[value] = prev(uniqueList.end());
}
}
};
/*
PROBLEM 8: Design a Food Rating System (LeetCode 2353)
──────────────────────────────────────────────────────
changeRating(food, newRating), highestRated(cuisine)
Time: O(log n) | Space: O(n)
*/
class FoodRatings {
unordered_map<string, pair<string, int>> foodInfo; // food → {cuisine, rating}
unordered_map<string, set<pair<int, string>>> cuisineRatings; // cuisine → {(-rating, food)}
public:
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
for (int i = 0; i < foods.size(); i++) {
foodInfo[foods[i]] = {cuisines[i], ratings[i]};
cuisineRatings[cuisines[i]].insert({-ratings[i], foods[i]});
}
}
void changeRating(string food, int newRating) {
auto& [cuisine, oldRating] = foodInfo[food];
cuisineRatings[cuisine].erase({-oldRating, food});
cuisineRatings[cuisine].insert({-newRating, food});
oldRating = newRating;
}
string highestRated(string cuisine) {
return cuisineRatings[cuisine].begin()->second;
}
};
// ============================================================================
// MAIN
// ============================================================================
int main() {
cout << "=== HashMap & Randomized Designs ===\n\n";
// 1. HashMap
MyHashMap hm;
hm.put(1, 1);
hm.put(2, 2);
cout << "1. HashMap get(1): " << hm.get(1) << "\n";
hm.remove(2);
cout << " HashMap get(2): " << hm.get(2) << " (removed)\n";
// 3. RandomizedSet
RandomizedSet rs;
rs.insert(1);
rs.insert(2);
rs.insert(3);
cout << "3. Random element: " << rs.getRandom() << "\n";
rs.remove(2);
cout << " After removing 2, random: " << rs.getRandom() << "\n";
// 5. Underground System
UndergroundSystem us;
us.checkIn(1, "A", 0);
us.checkOut(1, "B", 10);
us.checkIn(2, "A", 5);
us.checkOut(2, "B", 15);
cout << "5. Average A->B: " << us.getAverageTime("A", "B") << "\n";
return 0;
}
/*
================================================================================
SUMMARY
================================================================================
+───────────────────────────────+────────────────────────────────────────────────+
| Design | Key Technique |
+───────────────────────────────+────────────────────────────────────────────────+
| HashMap | Array of buckets + chaining |
| RandomizedSet | Array + HashMap (swap with last on delete) |
| RandomizedSet (dups) | Array + HashMap<val, set<indices>> |
| Underground System | Two HashMaps (checkins, journey stats) |
| Number Container | HashMap + ordered set per number |
| First Unique | List + HashMap to iterator |
+───────────────────────────────+────────────────────────────────────────────────+
================================================================================
*/