forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomized_set.py
More file actions
77 lines (60 loc) · 1.86 KB
/
randomized_set.py
File metadata and controls
77 lines (60 loc) · 1.86 KB
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
"""
Randomized Set
Design a data structure that supports insert, remove, and getRandom
in average O(1) time. Uses a list for random access and a dictionary
for O(1) lookup/removal.
Reference: https://leetcode.com/problems/insert-delete-getrandom-o1/
Complexity:
Time: O(1) average for insert, remove, get_random
Space: O(n)
"""
from __future__ import annotations
import random
class RandomizedSet:
"""A set supporting O(1) insert, remove, and random element access.
Examples:
>>> rs = RandomizedSet()
>>> rs.insert(1)
True
>>> rs.insert(1)
False
>>> rs.remove(1)
True
"""
def __init__(self) -> None:
"""Initialize the randomized set."""
self.nums: list[int] = []
self.idxs: dict[int, int] = {}
def insert(self, val: int) -> bool:
"""Insert a value into the set.
Args:
val: Value to insert.
Returns:
True if the value was inserted, False if already present.
"""
if val not in self.idxs:
self.nums.append(val)
self.idxs[val] = len(self.nums) - 1
return True
return False
def remove(self, val: int) -> bool:
"""Remove a value from the set.
Args:
val: Value to remove.
Returns:
True if the value was removed, False if not present.
"""
if val in self.idxs:
idx, last = self.idxs[val], self.nums[-1]
self.nums[idx], self.idxs[last] = last, idx
self.nums.pop()
self.idxs.pop(val, 0)
return True
return False
def get_random(self) -> int:
"""Return a random element from the set.
Returns:
A randomly chosen element.
"""
idx = random.randint(0, len(self.nums) - 1)
return self.nums[idx]