-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
count-zero-request-servers.py
68 lines (63 loc) · 1.91 KB
/
count-zero-request-servers.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
# Time: O(nlogn + mlogm)
# Space: O(n + m)
# sort, two pointers
class Solution(object):
def countServers(self, n, logs, x, queries):
"""
:type n: int
:type logs: List[List[int]]
:type x: int
:type queries: List[int]
:rtype: List[int]
"""
logs.sort(key=lambda x:x[1])
result = [0]*len(queries)
cnt = [0]*n
curr = left = right = 0
for t, i in sorted((t, i) for i, t in enumerate(queries)):
while right < len(logs) and logs[right][1] <= t:
if cnt[logs[right][0]-1] == 0:
curr += 1
cnt[logs[right][0]-1] += 1
right += 1
while left < right and logs[left][1] < t-x:
cnt[logs[left][0]-1] -= 1
if cnt[logs[left][0]-1] == 0:
curr -= 1
left += 1
result[i] = n-curr
return result
# Time: O(nlogn + mlogm)
# Space: O(n + m)
# sort, line sweep
class Solution2(object):
def countServers(self, n, logs, x, queries):
"""
:type n: int
:type logs: List[List[int]]
:type x: int
:type queries: List[int]
:rtype: List[int]
"""
events = []
for sid, t in logs:
events.append((t, +1, sid-1))
events.append((t+x+1, -1, sid-1))
events.append((float("inf"), 0, 0))
events.sort()
events2 = []
for i, t in enumerate(queries):
events2.append((t, i))
events2.sort(reverse=True)
result = [0]*len(queries)
cnt = [0]*n
curr = 0
for t, c, i in events:
while events2 and events2[-1][0] < t:
result[events2.pop()[1]] += n-curr
if cnt[i] == 0:
curr += 1
cnt[i] += c
if cnt[i] == 0:
curr -= 1
return result