forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1348.py
26 lines (19 loc) · 882 Bytes
/
1348.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
class TweetCounts:
def __init__(self):
self.tweets = collections.defaultdict(list)
def recordTweet(self, tweetName: str, time: int) -> None:
bisect.insort_left(self.tweets[tweetName], time)
def getTweetCountsPerFrequency(self, freq: str, tweetName: str, startTime: int, endTime: int) -> List[int]:
delta = 1
if freq == 'minute':
delta = 60
elif freq == 'hour':
delta = 60 * 60
elif freq == 'day':
delta = 60 * 60 * 24
res = [0] * ((endTime - startTime) // delta + 1)
pos = bisect.bisect_left(self.tweets[tweetName], startTime)
while pos < len(self.tweets[tweetName]) and self.tweets[tweetName][pos] <= endTime:
res[(self.tweets[tweetName][pos] - startTime) // delta] += 1
pos += 1
return res