-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertion_deletion.cpp
101 lines (87 loc) · 2.57 KB
/
insertion_deletion.cpp
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
// leetcode problem order of 1 insertion and deletion
#include<iostream>
#include<vector>
#include<unordered_map>
#include<unordered_set>
using namespace std;
class RandomizedCollection {
private:
vector<int> nums;
unordered_map<int, unordered_set<int>> m;
public:
RandomizedCollection() {
// Constructor initializes the data structures.
}
bool insert(int val) {
bool alreadyExists = m.find(val) != m.end() && !m[val].empty();
nums.push_back(val);
m[val].insert(nums.size() - 1);
return !alreadyExists;
}
bool remove(int val) {
if (m.find(val) == m.end() || m[val].empty()) {
return false;
}
int pos = *m[val].begin();
int lastElement = nums.back();
int lastPos = nums.size() - 1;
if (val == lastElement) {
// Special case: the element to be removed is the same as the last element
nums.pop_back(); // Remove the last element from `nums`
m[val].erase(lastPos);
if (m[val].empty()) {
m.erase(val);
}
} else {
// General case: the element to be removed is not the last element
nums[pos] = lastElement; // Move the last element to the position of `val`
nums.pop_back(); // Remove the last element from `nums`
m[lastElement].insert(pos);
m[lastElement].erase(lastPos);
m[val].erase(pos);
if (m[val].empty()) {
m.erase(val);
}
}
return true;
}
int getRandom() {
int randomIndex = rand() % nums.size();
return nums[randomIndex];
}
void display()
{
for(int i=0; i<nums.size(); i++)
{
cout << nums[i] << " ";
}
cout << endl;
}
};
/**
* Your RandomizedCollection object will be instantiated and called as such:
* RandomizedCollection* obj = new RandomizedCollection();
* bool param_1 = obj->insert(val);
* bool param_2 = obj->remove(val);
* int param_3 = obj->getRandom();
*/
int main()
{
RandomizedCollection* obj = new RandomizedCollection();
bool param_1 = obj->insert(1);
bool param_2 = obj->insert(2);
bool param_4 = obj->insert(4);
bool param_5 = obj->insert(5);
bool param_12 = obj->insert(1);
bool param_13 = obj->insert(2);
bool param_3 = obj->insert(3);
obj->display();
bool param_6 = obj->remove(3);
bool param_7 = obj->remove(4);
bool param_8 = obj->remove(5);
bool param_9 = obj->remove(2);
bool param_10 = obj->remove(1);
obj->display();
int param_11 = obj->getRandom();
return 0;
}