-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |