Skip to content

Commit 448d174

Browse files
380. Insert Delete GetRandom O(1)
Difficulty: Medium 18 / 18 test cases passed. Runtime: 556 ms Memory Usage: 17.9 MB
1 parent ca118b2 commit 448d174

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""
2+
Implement the RandomizedSet class:
3+
- bool insert(int val) Inserts an item val into the set if not present.
4+
Returns true if the item was not present, false otherwise.
5+
- bool remove(int val) Removes an item val from the set if present.
6+
Returns true if the item was present, false otherwise.
7+
- int getRandom() Returns a random element from the current set of elements
8+
(it's guaranteed that at least one element exists when this method is
9+
called). Each element must have the same probability of being returned.
10+
11+
Follow up: Could you implement the functions of the class with each function
12+
works in average O(1) time?
13+
14+
Example:
15+
Input
16+
["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
17+
[[], [1], [2], [2], [], [1], [2], []]
18+
Output
19+
[null, true, false, true, 2, true, false, 2]
20+
Explanation
21+
RandomizedSet randomizedSet = new RandomizedSet();
22+
randomizedSet.insert(1); // Inserts 1 to the set. Returns true
23+
// as 1 was inserted successfully.
24+
randomizedSet.remove(2); // Returns false as 2 does not exist
25+
// in the set.
26+
randomizedSet.insert(2); // Inserts 2 to the set, returns true.
27+
// Set now contains [1,2].
28+
randomizedSet.getRandom(); // getRandom() should return either
29+
// 1 or 2 randomly.
30+
randomizedSet.remove(1); // Removes 1 from the set, returns true.
31+
// Set now contains [2].
32+
randomizedSet.insert(2); // 2 was already in the set, so return
33+
// false.
34+
randomizedSet.getRandom(); // Since 2 is the only number in the
35+
// set, getRandom() will always
36+
// return 2.
37+
38+
Constraints:
39+
- -2**31 <= val <= 2**31 - 1
40+
- At most 105 calls will be made to insert, remove, and getRandom.
41+
- There will be at least one element in the data structure when
42+
getRandom is called.
43+
"""
44+
#Difficulty: Medium
45+
#18 / 18 test cases passed.
46+
#Runtime: 556 ms
47+
#Memory Usage: 17.9 MB
48+
49+
#Runtime: 556 ms, faster than 8.03% of Python3 online submissions for Insert Delete GetRandom O(1).
50+
#Memory Usage: 17.9 MB, less than 62.45% of Python3 online submissions for Insert Delete GetRandom O(1).
51+
52+
import random
53+
54+
class RandomizedSet:
55+
56+
def __init__(self):
57+
"""
58+
Initialize your data structure here.
59+
"""
60+
self.set = []
61+
62+
def insert(self, val: int) -> bool:
63+
"""
64+
Inserts a value to the set. Returns true if the set did not already
65+
contain the specified element.
66+
"""
67+
if val not in self.set:
68+
self.set.append(val)
69+
return True
70+
return False
71+
72+
def remove(self, val: int) -> bool:
73+
"""
74+
Removes a value from the set. Returns true if the set contained the
75+
specified element.
76+
"""
77+
if val in self.set:
78+
self.set.remove(val)
79+
return True
80+
return False
81+
82+
def getRandom(self) -> int:
83+
"""
84+
Get a random element from the set.
85+
"""
86+
return random.choice(self.set)
87+
88+
89+
# Your RandomizedSet object will be instantiated and called as such:
90+
# obj = RandomizedSet()
91+
# param_1 = obj.insert(val)
92+
# param_2 = obj.remove(val)
93+
# param_3 = obj.getRandom()

0 commit comments

Comments
 (0)