From 2a89752532f7dfea51b48eed8787ce58350557d0 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Tue, 16 Jan 2024 12:33:41 -0500 Subject: 380 --- 380/main.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 380/main.py 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() -- cgit v1.2.3