Skip to content

Commit abc0ac7

Browse files
committed
[LeetCode Sync] Runtime - 167 ms (50.55%), Memory - 101.9 MB (93.93%)
1 parent e61f641 commit abc0ac7

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<p>Implement the <code>RandomizedSet</code> class:</p>
2+
3+
<ul>
4+
<li><code>RandomizedSet()</code> Initializes the <code>RandomizedSet</code> object.</li>
5+
<li><code>bool insert(int val)</code> Inserts an item <code>val</code> into the set if not present. Returns <code>true</code> if the item was not present, <code>false</code> otherwise.</li>
6+
<li><code>bool remove(int val)</code> Removes an item <code>val</code> from the set if present. Returns <code>true</code> if the item was present, <code>false</code> otherwise.</li>
7+
<li><code>int getRandom()</code> Returns a random element from the current set of elements (it&#39;s guaranteed that at least one element exists when this method is called). Each element must have the <b>same probability</b> of being returned.</li>
8+
</ul>
9+
10+
<p>You must implement the functions of the class such that each function works in&nbsp;<strong>average</strong>&nbsp;<code>O(1)</code>&nbsp;time complexity.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
15+
<pre>
16+
<strong>Input</strong>
17+
[&quot;RandomizedSet&quot;, &quot;insert&quot;, &quot;remove&quot;, &quot;insert&quot;, &quot;getRandom&quot;, &quot;remove&quot;, &quot;insert&quot;, &quot;getRandom&quot;]
18+
[[], [1], [2], [2], [], [1], [2], []]
19+
<strong>Output</strong>
20+
[null, true, false, true, 2, true, false, 2]
21+
22+
<strong>Explanation</strong>
23+
RandomizedSet randomizedSet = new RandomizedSet();
24+
randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
25+
randomizedSet.remove(2); // Returns false as 2 does not exist in the set.
26+
randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
27+
randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly.
28+
randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2].
29+
randomizedSet.insert(2); // 2 was already in the set, so return false.
30+
randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2.
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
<p><strong>Constraints:</strong></p>
35+
36+
<ul>
37+
<li><code>-2<sup>31</sup> &lt;= val &lt;= 2<sup>31</sup> - 1</code></li>
38+
<li>At most <code>2 *&nbsp;</code><code>10<sup>5</sup></code> calls will be made to <code>insert</code>, <code>remove</code>, and <code>getRandom</code>.</li>
39+
<li>There will be <strong>at least one</strong> element in the data structure when <code>getRandom</code> is called.</li>
40+
</ul>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class RandomizedSet {
2+
public:
3+
unordered_map<int,int> mp;
4+
vector<int> l;
5+
6+
RandomizedSet() {}
7+
8+
bool insert(int val) {
9+
bool res = mp.find(val) == mp.end();
10+
if (res) {
11+
mp[val] = l.size();
12+
l.push_back(val);
13+
}
14+
return res;
15+
}
16+
17+
bool remove(int val) {
18+
bool res = mp.find(val) != mp.end();
19+
if (res) {
20+
int idx = mp[val];
21+
int lastVal = l.back();
22+
l[idx] = lastVal;
23+
l.pop_back();
24+
mp[lastVal] = idx;
25+
mp.erase(val);
26+
}
27+
return res;
28+
}
29+
30+
int getRandom() {
31+
return l[rand() % l.size()];
32+
}
33+
};
34+
35+
/**
36+
* Your RandomizedSet object will be instantiated and called as such:
37+
* RandomizedSet* obj = new RandomizedSet();
38+
* bool param_1 = obj->insert(val);
39+
* bool param_2 = obj->remove(val);
40+
* int param_3 = obj->getRandom();
41+
*/

0 commit comments

Comments
 (0)