Skip to content

Commit 74ae25b

Browse files
committed
2019-06-29
1 parent 86cbc47 commit 74ae25b

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from collections import deque
2+
class HitCounter(object):
3+
4+
def __init__(self):
5+
"""
6+
Initialize your data structure here.
7+
"""
8+
self.queue = deque()
9+
10+
def hit(self, timestamp):
11+
"""
12+
Record a hit.
13+
@param timestamp - The current timestamp (in seconds granularity).
14+
:type timestamp: int
15+
:rtype: None
16+
"""
17+
self.queue.append(timestamp)
18+
19+
def getHits(self, timestamp):
20+
"""
21+
Return the number of hits in the past 5 minutes.
22+
@param timestamp - The current timestamp (in seconds granularity).
23+
:type timestamp: int
24+
:rtype: int
25+
"""
26+
while self.queue and timestamp - self.queue[0] > 299:
27+
self.queue.popleft()
28+
return len(self.queue)
29+
30+
31+
32+
# Your HitCounter object will be instantiated and called as such:
33+
# obj = HitCounter()
34+
# obj.hit(timestamp)
35+
# param_2 = obj.getHits(timestamp)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class UnionFindSet(object):
2+
def __init__(self, n):
3+
self.roots = [i for i in range(n)]
4+
self.rank = [0 for i in range(n)]
5+
self.count = 0
6+
7+
def find(self, member):
8+
tmp = []
9+
while member != self.roots[member]:
10+
tmp.append(member)
11+
member = self.roots[member]
12+
for root in tmp:
13+
self.roots[root] = member
14+
return member
15+
16+
def union(self, p, q):
17+
parentP = self.find(p)
18+
parentQ = self.find(q)
19+
if parentP != parentQ:
20+
if self.rank[parentP] > self.rank[parentQ]:
21+
self.roots[parentQ] = parentP
22+
elif self.rank[parentP] < self.rank[parentQ]:
23+
self.roots[parentP] = parentQ
24+
else:
25+
self.roots[parentQ] = parentP
26+
self.rank[parentP] -= 1
27+
self.count -= 1
28+
29+
def check(self):
30+
print self.roots
31+
flag = self.find(self.roots[0])
32+
for i in self.roots:
33+
if self.find(i) != flag:
34+
return False
35+
return True
36+
37+
class Solution(object):
38+
def earliestAcq(self, logs, N):
39+
"""
40+
:type logs: List[List[int]]
41+
:type N: int
42+
:rtype: int
43+
"""
44+
logs = sorted(logs, key = lambda x:x[0])
45+
ufs = UnionFindSet(N)
46+
for time, x, y in logs:
47+
ufs.union(x, y)
48+
if ufs.check():
49+
return time
50+
return -1

0 commit comments

Comments
 (0)