---
# System prepended metadata

title: 2747. Count Zero Request Servers
tags: [Leetcode, Sliding Window, Medium]

---

Link: https://leetcode.com/problems/count-zero-request-servers/description/
## 思路
fixed size sliding window
## Code
```python=
class Solution:
    def countServers(self, n: int, logs: List[List[int]], x: int, queries: List[int]) -> List[int]:
        ans = [0]*len(queries)
        count = defaultdict(int)
        left, right = 0, 0
        used = 0
        logs.sort(key=lambda l:l[1])
        for [t, idx] in sorted([t, idx] for idx, t in enumerate(queries)):
            while right<len(logs) and logs[right][1]<=t:
                currId = logs[right][0]
                count[currId] += 1
                if count[currId]==1: used += 1
                right += 1
            while left<right and logs[left][1]<t-x:
                currId = logs[left][0]
                count[currId] -= 1
                if count[currId]==0: used -= 1
                left += 1
            ans[idx] = n-used
        return ans
```