aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorterminaldweller <devi@terminaldweller.com>2024-01-16 17:33:41 +0000
committerterminaldweller <devi@terminaldweller.com>2024-01-16 17:33:41 +0000
commit2a89752532f7dfea51b48eed8787ce58350557d0 (patch)
tree9ad4bcd5e2bc3b711e8dfed4d055b5c37316fad6
parent2225 (diff)
downloadleetcode-2a89752532f7dfea51b48eed8787ce58350557d0.tar.gz
leetcode-2a89752532f7dfea51b48eed8787ce58350557d0.zip
380
-rwxr-xr-x380/main.py42
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()