-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.py
64 lines (53 loc) · 1.86 KB
/
Solution.py
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
class Node:
__slots__ = ['val', 'next']
def __init__(self, val: int, level: int):
self.val = val
self.next = [None] * level
class Skiplist:
max_level = 32
p = 0.25
def __init__(self):
self.head = Node(-1, self.max_level)
self.level = 0
def search(self, target: int) -> bool:
curr = self.head
for i in range(self.level - 1, -1, -1):
curr = self.find_closest(curr, i, target)
if curr.next[i] and curr.next[i].val == target:
return True
return False
def add(self, num: int) -> None:
curr = self.head
level = self.random_level()
node = Node(num, level)
self.level = max(self.level, level)
for i in range(self.level - 1, -1, -1):
curr = self.find_closest(curr, i, num)
if i < level:
node.next[i] = curr.next[i]
curr.next[i] = node
def erase(self, num: int) -> bool:
curr = self.head
ok = False
for i in range(self.level - 1, -1, -1):
curr = self.find_closest(curr, i, num)
if curr.next[i] and curr.next[i].val == num:
curr.next[i] = curr.next[i].next[i]
ok = True
while self.level > 1 and self.head.next[self.level - 1] is None:
self.level -= 1
return ok
def find_closest(self, curr: Node, level: int, target: int) -> Node:
while curr.next[level] and curr.next[level].val < target:
curr = curr.next[level]
return curr
def random_level(self) -> int:
level = 1
while level < self.max_level and random.random() < self.p:
level += 1
return level
# Your Skiplist object will be instantiated and called as such:
# obj = Skiplist()
# param_1 = obj.search(target)
# obj.add(num)
# param_3 = obj.erase(num)