Skip to content

Commit

Permalink
Time: 413 ms (14.83%), Space: 96.9 MB (83.21%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
arpitkekri committed Oct 21, 2021
1 parent 7a8c531 commit 209d1ab
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions insert-delete-getrandom-o1/insert-delete-getrandom-o1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class RandomizedSet {
public:
unordered_set<int> s;
RandomizedSet() {
s.clear();
}

bool insert(int val) {
if(s.count(val))
return false;
s.insert(val);
return true;
}

bool remove(int val) {
if(s.count(val)) {
s.erase(val);
return true;
}
return false;
}

int getRandom() {
int n = s.size();
int randNo = rand()%n;
auto itr = s.begin();
advance(itr, randNo);
return *itr;
}
};

/**
* Your RandomizedSet object will be instantiated and called as such:
* RandomizedSet* obj = new RandomizedSet();
* bool param_1 = obj->insert(val);
* bool param_2 = obj->remove(val);
* int param_3 = obj->getRandom();
*/

0 comments on commit 209d1ab

Please sign in to comment.