-
Notifications
You must be signed in to change notification settings - Fork 0
/
Design.py
162 lines (130 loc) · 4.61 KB
/
Design.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# 251. Flatten 2D Vector
class Vector2D:
def __init__(self, vec: List[List[int]]):
self.vec = []
for row in vec:
for val in row:
self.vec.append(val)
self.index = 0
def next(self) -> int:
ret = self.vec[self.index]
self.index += 1
return ret
def hasNext(self) -> bool:
if len(self.vec) == self.index:
return False
return True
# Your Vector2D object will be instantiated and called as such:
# obj = Vector2D(vec)
# param_1 = obj.next()
# param_2 = obj.hasNext()
class Vector2D:
def __init__(self, v: List[List[int]]):
self.vector = v
self.inner = 0
self.outer = 0
# If the current outer and inner point to an integer, this method does nothing.
# Otherwise, inner and outer are advanced until they point to an integer.
# If there are no more integers, then outer will be equal to vector.length
# when this method terminates.
def advance_to_next(self):
# While outer is still within the vector, but inner is over the
# end of the inner list pointed to by outer, we want to move
# forward to the start of the next inner vector.
while self.outer < len(self.vector) and self.inner == len(self.vector[self.outer]):
self.outer += 1
self.inner = 0
def next(self) -> int:
# Ensure the position pointers are moved such they point to an integer,
# or put outer = vector.length.
self.advance_to_next()
# Return current element and move inner so that is after the current
# element.
result = self.vector[self.outer][self.inner]
self.inner += 1
return result
def hasNext(self) -> bool:
# Ensure the position pointers are moved such they point to an integer,
# or put outer = vector.length.
self.advance_to_next()
# If outer = vector.length then there are no integers left, otherwise
# we've stopped at an integer and so there's an integer left.
return self.outer < len(self.vector)
# 1166. Design File System
# Dictionary
class FileSystem:
def __init__(self):
self.paths = defaultdict()
def createPath(self, path: str, value: int) -> bool:
# Step-1: basic path validations
if path == "/" or len(path) == 0 or path in self.paths:
return False
# Step-2: if the parent doesn't exist. Note that "/" is a valid parent.
parent = path[:path.rfind('/')]
if len(parent) > 1 and parent not in self.paths:
return False
# Step-3: add this new path and return true.
self.paths[path] = value
return True
def get(self, path: str) -> int:
return self.paths.get(path, -1)
# Trie
class FileSystem:
def __init__(self):
self.root = {}
def createPath(self, path: str, value: int) -> bool:
components = path.split('/')
cur = self.root
for i in range(1, len(components)):
name = components[i]
# print(name, i)
if name not in cur:
if i == len(components) - 1:
cur[name] = {}
else:
return False
cur = cur[name]
if "value" in cur:
return False
cur["value"] = value
return True
def get(self, path: str) -> int:
components = path.split('/')
cur = self.root
for i in range(1, len(components)):
name = components[i]
if name not in cur:
return -1
cur = cur[name]
return cur["value"]
# Your FileSystem object will be instantiated and called as such:
# obj = FileSystem()
# param_1 = obj.createPath(path,value)
# param_2 = obj.get(path)
# 380. Insert Delete GetRandom O(1)
class RandomizedSet:
def __init__(self):
self.dict = {}
self.list = []
def insert(self, val: int) -> bool:
if val in self.dict:
return False
self.dict[val] = len(self.list)
self.list.append(val)
return True
def remove(self, val: int) -> bool:
if val not in self.dict:
return False
idx = self.dict[val]
self.dict[self.list[-1]] = idx
del self.dict[val]
self.list[idx], self.list[-1] = self.list[-1], self.list[idx]
self.list.pop()
return True
def getRandom(self) -> int:
return choice(self.list)
# Your RandomizedSet object will be instantiated and called as such:
# obj = RandomizedSet()
# param_1 = obj.insert(val)
# param_2 = obj.remove(val)
# param_3 = obj.getRandom()