diff options
Diffstat (limited to '380/main.py')
-rwxr-xr-x | 380/main.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/380/main.py b/380/main.py new file mode 100755 index 0000000..9077324 --- /dev/null +++ b/380/main.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python + +import random + + +class RandomizedSet: + def __init__(self): + self.lst = [] + self.idx_map = {} + + def search(self, val): + return val in self.idx_map + + def insert(self, val): + if self.search(val): + return False + + self.lst.append(val) + self.idx_map[val] = len(self.lst) - 1 + return True + + def remove(self, val): + if not self.search(val): + return False + + idx = self.idx_map[val] + self.lst[idx] = self.lst[-1] + self.idx_map[self.lst[-1]] = idx + self.lst.pop() + del self.idx_map[val] + return True + + def getRandom(self): + return random.choice(self.lst) + + +def main(): + pass + + +if __name__ == "__main__": + main() |