Insert Delete GetRandom O(1)
Implement the RandomizedSet class:
RandomizedSet() Initializes the RandomizedSet object.
bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.
bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.
int getRandom() Returns a random element from the current set of elements (it’s guaranteed that at least one element exists when this method is called). Each element must have the same probability of being returned.
You must implement the functions of the class such that each function works in average O(1) time complexity.
1 | Example 1: |
Use a combination of data structures
Create data structures
We use two data structures in this question, one is unordered_map
, the other is vector
.
1 | private: |
Initialise the class
1 | RandomizedSet() { |
Insert
For unordered_map
, it has a function called find (value)
. This function returns an iterator pointing to the found element or an iterator equal to the end() of the map if the element is not found. So we use this function to make sure that the value is not in the vector. If the vlaue is already in the vector, we return false. Otherwise, we push the value to the end of the vector, and record the index of it in unordered_map
.
1 | bool insert(int val) { |
Remove
Similarly, we first check if the value is in the vector. If it is not, we return false.
Then we swap the element of the target value with the end element. After that, we update the index of the end element in the map. Finally, we pop the end element from the vector and erase the target element in the map and return true.
1 | bool remove(int val) { |
Random return a value in the array
To return a value in the array randomly, we use rand()
function to generate a random number between 0 and the size of the vector. Then we return the element at the index of the random number.
1 | int getRandom() { |
Full code
1 | class RandomizedSet { |
About Unordered_map
An unordered_map
is a container in C++ that provides an associative array (or dictionary) data structure. It allows you to store key-value pairs where keys are unique, and you can quickly retrieve the corresponding values based on the keys. Here are some of the key features of std::unordered_map
:
Fast Lookup:
unordered_map
provides fast lookup of values based on their keys. The time complexity for average-case lookups is O(1), which makes it suitable for applications where you need efficient access to data based on keys.Unique Keys: Each key in an
unordered_map
is unique. This means that you cannot have multiple entries with the same key. If you attempt to insert a key that already exists, the new value will overwrite the old one.Hash-Based: The underlying data structure is based on a hash table, which uses a hash function to map keys to positions in memory. This allows for efficient retrieval and insertion of key-value pairs.
Iterating: You can easily iterate through all the key-value pairs in an
unordered_map
using iterators. This is useful for performing operations on all the elements or searching for specific key-value pairs.No Guaranteed Order: The key-value pairs are not guaranteed to be stored in any specific order. They are organized based on the hash values of the keys, so you may see different orderings when iterating through the map.
Dynamic Sizing:
unordered_map
dynamically resizes itself as you insert or erase elements. This ensures that it remains efficient as you add more data.Custom Key Types: You can use custom types as keys as long as you provide a hash function and equality operator for those types.
Here’s a simple example of how to use unordered_map
:
1 |
|
In this example, we create an unordered_map
to store ages based on names. It demonstrates inserting key-value pairs, accessing values using keys, and iterating through the map.