-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
112 lines (93 loc) · 3.25 KB
/
Copy pathsolution.cpp
File metadata and controls
112 lines (93 loc) · 3.25 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
/**
* Problem: 381. Insert Delete GetRandom O(1) - Duplicates allowed
* Difficulty: Hard
* Topics: Array, Hash Table, Math, Design, Randomized
* LeetCode Link: https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/
*
* Time Complexity: O(1) average for insert, remove, getRandom
* Space Complexity: O(N) - Storage for elements and index sets
*/
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <cstdlib>
#include <cassert>
using namespace std;
class RandomizedCollection {
private:
vector<int> nums; // Dense array for O(1) random access
unordered_map<int, unordered_set<int>> valToIdx; // val -> set of indices in nums[]
public:
RandomizedCollection() {}
bool insert(int val) {
bool notPresent = (valToIdx.find(val) == valToIdx.end() || valToIdx[val].empty());
nums.push_back(val);
valToIdx[val].insert(static_cast<int>(nums.size()) - 1);
return notPresent;
}
bool remove(int val) {
if (valToIdx.find(val) == valToIdx.end() || valToIdx[val].empty()) {
return false;
}
// Pick any index of val to remove
int removeIdx = *valToIdx[val].begin();
int lastIdx = static_cast<int>(nums.size()) - 1;
int lastVal = nums[lastIdx];
// Swap the element to remove with the last element
nums[removeIdx] = lastVal;
// Update index sets
valToIdx[val].erase(removeIdx);
valToIdx[lastVal].erase(lastIdx);
if (removeIdx != lastIdx) {
valToIdx[lastVal].insert(removeIdx);
}
nums.pop_back();
return true;
}
int getRandom() {
return nums[rand() % nums.size()];
}
};
// ==========================================
// Local Test Runner (Guarded for LeetCode Submission)
// ==========================================
#ifdef LOCAL_TEST
int main() {
RandomizedCollection rc;
// Test Case 1: Basic insert/remove/getRandom
{
assert(rc.insert(1) == true); // [1], returns true (new)
assert(rc.insert(1) == false); // [1,1], returns false (duplicate)
assert(rc.insert(2) == true); // [1,1,2], returns true (new)
int r = rc.getRandom();
assert(r == 1 || r == 2);
cout << "Test 1a Passed: insert and getRandom work" << endl;
assert(rc.remove(1) == true); // removes one 1: [1,2] or [2,1]
r = rc.getRandom();
assert(r == 1 || r == 2);
cout << "Test 1b Passed: remove and getRandom work" << endl;
}
// Test Case 2: Remove non-existent
{
RandomizedCollection rc2;
rc2.insert(10);
assert(rc2.remove(20) == false);
cout << "Test 2 Passed: remove non-existent returns false" << endl;
}
// Test Case 3: Insert and remove all
{
RandomizedCollection rc3;
rc3.insert(5);
rc3.insert(5);
rc3.insert(5);
assert(rc3.remove(5) == true);
assert(rc3.remove(5) == true);
assert(rc3.remove(5) == true);
assert(rc3.remove(5) == false);
cout << "Test 3 Passed: insert/remove all duplicates" << endl;
}
cout << "All test cases passed successfully!" << endl;
return 0;
}
#endif