Skip to content

Commit

Permalink
2019-06-29
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Jun 29, 2019
1 parent 86cbc47 commit 74ae25b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 0362.敲击计数器/0362-敲击计数器.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from collections import deque
class HitCounter(object):

def __init__(self):
"""
Initialize your data structure here.
"""
self.queue = deque()

def hit(self, timestamp):
"""
Record a hit.
@param timestamp - The current timestamp (in seconds granularity).
:type timestamp: int
:rtype: None
"""
self.queue.append(timestamp)

def getHits(self, timestamp):
"""
Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity).
:type timestamp: int
:rtype: int
"""
while self.queue and timestamp - self.queue[0] > 299:
self.queue.popleft()
return len(self.queue)



# Your HitCounter object will be instantiated and called as such:
# obj = HitCounter()
# obj.hit(timestamp)
# param_2 = obj.getHits(timestamp)
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class UnionFindSet(object):
def __init__(self, n):
self.roots = [i for i in range(n)]
self.rank = [0 for i in range(n)]
self.count = 0

def find(self, member):
tmp = []
while member != self.roots[member]:
tmp.append(member)
member = self.roots[member]
for root in tmp:
self.roots[root] = member
return member

def union(self, p, q):
parentP = self.find(p)
parentQ = self.find(q)
if parentP != parentQ:
if self.rank[parentP] > self.rank[parentQ]:
self.roots[parentQ] = parentP
elif self.rank[parentP] < self.rank[parentQ]:
self.roots[parentP] = parentQ
else:
self.roots[parentQ] = parentP
self.rank[parentP] -= 1
self.count -= 1

def check(self):
print self.roots
flag = self.find(self.roots[0])
for i in self.roots:
if self.find(i) != flag:
return False
return True

class Solution(object):
def earliestAcq(self, logs, N):
"""
:type logs: List[List[int]]
:type N: int
:rtype: int
"""
logs = sorted(logs, key = lambda x:x[0])
ufs = UnionFindSet(N)
for time, x, y in logs:
ufs.union(x, y)
if ufs.check():
return time
return -1

0 comments on commit 74ae25b

Please sign in to comment.