Count Zero Request Servers
Count Zero Request Servers is a medium arrays & hashing problem solved with the sliding window · hash map pattern.
The best approach, optimal (sorted queries with a sliding window), runs in O(L log L + Q log Q + n) time and O(n + Q) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from scan all logs for each query up.
Problem
You run n servers numbered 1 to n. The array logs records incoming requests: logs[i] = [serverId, time] means server serverId received a request at time time. The logs are not necessarily in time order.
You are also given an integer x and an array queries. For each query q, look at the closed time window [q - x, q] (both ends included) and count how many servers received no request at all during it. Return an array ans where ans[i] is that count for queries[i].
Examples
Example 1
- Input
n = 4, logs = [[1, 2], [2, 5], [1, 7], [3, 6]], x = 3, queries = [5, 8, 11]- Output
[2, 1, 4]- Why
- For q = 5 the window is [2, 5]: servers 1 and 2 were busy, so servers 3 and 4 were idle. For q = 8 the window [5, 8] holds requests to servers 2, 3 and 1, leaving only server 4. For q = 11 the window [8, 11] holds no requests, so all 4 servers were idle.
Example 2
- Input
n = 2, logs = [[2, 3], [2, 4]], x = 1, queries = [2, 4]- Output
[2, 1]- Why
- The window [1, 2] contains no request. The window [3, 4] contains two requests, both to server 2, so only server 1 was idle.
Example 3
- Input
n = 3, logs = [[1, 10], [2, 10], [3, 10]], x = 5, queries = [10, 16]- Output
[0, 3]- Why
- Time 10 lies inside [5, 10], so every server was busy. The window [11, 16] starts after time 10, so every server was idle.
Constraints
1 <= n <= 10^51 <= logs.length <= 10^51 <= queries.length <= 10^5logs[i].length == 21 <= logs[i][0] <= n1 <= logs[i][1] <= 10^61 <= x <= 10^5x < queries[i] <= 10^6
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (3 examples, or your own). Submit also runs 42 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Scan all logs for each query | O(Q · (L + n)) | O(n) |
| Optimal (sorted queries with a sliding window) | O(L log L + Q log Q + n) | O(n + Q) |
1Scan all logs for each query
O(Q · (L + n))Each of the Q queries scans all L logs and allocates a busy array of size n.O(n)The busy array for the current query (plus the output).Answer each query on its own: walk through every log, and mark a server as busy the first time one of its requests falls inside [q - x, q]. Servers never marked were idle.
- For each query q, start with count = n and a fresh busy array.
- For each log [s, t] with q - x <= t <= q: if s is not busy yet, mark it and decrease count.
- The answer for q is count.
class Solution {
public int[] countServers(int n, int[][] logs, int x, int[] queries) {
int[] ans = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
boolean[] busy = new boolean[n + 1];
int count = n;
for (int[] log : logs) {
int t = log[1];
if (t >= queries[i] - x && t <= queries[i] && !busy[log[0]]) {
busy[log[0]] = true;
count--;
}
}
ans[i] = count;
}
return ans;
}
}class Solution:
def countServers(self, n: int, logs: List[List[int]], x: int, queries: List[int]) -> List[int]:
ans = [0] * len(queries)
for i, q in enumerate(queries):
busy = [False] * (n + 1)
count = n
for s, t in logs:
if q - x <= t <= q and not busy[s]:
busy[s] = True
count -= 1
ans[i] = count
return ansclass Solution {
public:
vector<int> countServers(int n, vector<vector<int>>& logs, int x, vector<int>& queries) {
vector<int> ans(queries.size());
for (int i = 0; i < (int)queries.size(); i++) {
vector<bool> busy(n + 1, false);
int count = n;
for (auto& log : logs) {
int t = log[1];
if (t >= queries[i] - x && t <= queries[i] && !busy[log[0]]) {
busy[log[0]] = true;
count--;
}
}
ans[i] = count;
}
return ans;
}
};function countServers(n, logs, x, queries) {
const ans = new Array(queries.length).fill(0);
for (let i = 0; i < queries.length; i++) {
const busy = new Array(n + 1).fill(false);
let count = n;
for (const log of logs) {
const t = log[1];
if (t >= queries[i] - x && t <= queries[i] && !busy[log[0]]) {
busy[log[0]] = true;
count--;
}
}
ans[i] = count;
}
return ans;
}2Optimal (sorted queries with a sliding window)
O(L log L + Q log Q + n)Two sorts; then each log enters and leaves the window once, and each query is answered in O(1) after the pointers move.O(n + Q)The per-server counters, the sorted query order and the answer array.Sort the logs by time and visit the queries in increasing order of q. The window [q - x, q] then only slides to the right, so two pointers over the sorted logs can add requests entering on the right and drop requests leaving on the left. A per-server counter tells when a server becomes busy (count goes 0 to 1) or idle again (1 to 0), and active holds how many servers are busy right now.
- Sort logs by time. Sort the query indexes by their value.
- Keep cnt[server], active = 0, and pointers lo = hi = 0 into the logs.
- For each query q in increasing order: while logs[hi] has time <= q, add it (if cnt goes from 0 to 1, active++).
- While logs[lo] has time < q - x, remove it (if cnt drops to 0, active--).
- Store n - active at the query's original index.
class Solution {
public int[] countServers(int n, int[][] logs, int x, int[] queries) {
Arrays.sort(logs, (a, b) -> Integer.compare(a[1], b[1]));
Integer[] order = new Integer[queries.length];
for (int i = 0; i < order.length; i++) order[i] = i;
Arrays.sort(order, (a, b) -> Integer.compare(queries[a], queries[b]));
int[] cnt = new int[n + 1];
int[] ans = new int[queries.length];
int lo = 0, hi = 0, active = 0;
for (int qi : order) {
int q = queries[qi];
while (hi < logs.length && logs[hi][1] <= q) {
if (cnt[logs[hi][0]]++ == 0) active++;
hi++;
}
while (lo < hi && logs[lo][1] < q - x) {
if (--cnt[logs[lo][0]] == 0) active--;
lo++;
}
ans[qi] = n - active;
}
return ans;
}
}class Solution:
def countServers(self, n: int, logs: List[List[int]], x: int, queries: List[int]) -> List[int]:
logs = sorted(logs, key=lambda e: e[1])
order = sorted(range(len(queries)), key=lambda i: queries[i])
cnt = [0] * (n + 1)
ans = [0] * len(queries)
lo = hi = active = 0
for qi in order:
q = queries[qi]
while hi < len(logs) and logs[hi][1] <= q:
if cnt[logs[hi][0]] == 0:
active += 1
cnt[logs[hi][0]] += 1
hi += 1
while lo < hi and logs[lo][1] < q - x:
cnt[logs[lo][0]] -= 1
if cnt[logs[lo][0]] == 0:
active -= 1
lo += 1
ans[qi] = n - active
return ansclass Solution {
public:
vector<int> countServers(int n, vector<vector<int>>& logs, int x, vector<int>& queries) {
sort(logs.begin(), logs.end(), [](const vector<int>& a, const vector<int>& b) { return a[1] < b[1]; });
vector<int> order(queries.size());
for (int i = 0; i < (int)order.size(); i++) order[i] = i;
sort(order.begin(), order.end(), [&](int a, int b) { return queries[a] < queries[b]; });
vector<int> cnt(n + 1, 0);
vector<int> ans(queries.size());
int lo = 0, hi = 0, active = 0;
for (int qi : order) {
int q = queries[qi];
while (hi < (int)logs.size() && logs[hi][1] <= q) {
if (cnt[logs[hi][0]]++ == 0) active++;
hi++;
}
while (lo < hi && logs[lo][1] < q - x) {
if (--cnt[logs[lo][0]] == 0) active--;
lo++;
}
ans[qi] = n - active;
}
return ans;
}
};function countServers(n, logs, x, queries) {
logs = [...logs].sort((a, b) => a[1] - b[1]);
const order = queries.map((_, i) => i);
order.sort((a, b) => queries[a] - queries[b]);
const cnt = new Array(n + 1).fill(0);
const ans = new Array(queries.length).fill(0);
let lo = 0, hi = 0, active = 0;
for (const qi of order) {
const q = queries[qi];
while (hi < logs.length && logs[hi][1] <= q) {
if (cnt[logs[hi][0]]++ === 0) active++;
hi++;
}
while (lo < hi && logs[lo][1] < q - x) {
if (--cnt[logs[lo][0]] === 0) active--;
lo++;
}
ans[qi] = n - active;
}
return ans;
}Edge cases to test
- A request exactly at q - x or exactly at q (both ends are inclusive)
- Several requests to the same server inside one window (it counts once)
- A window with no requests at all (answer n)
- Queries given out of order or repeated
- Logs not sorted by time
Hints
Hint 1
For a single query, the answer is n minus the number of distinct servers with a request in [q - x, q].
Hint 2
If you answer the queries in increasing order of q, both ends of the window only move to the right.
Hint 3
Sort the logs by time and keep a count of requests per server inside the window, plus how many servers have a count above zero.
FAQ
What is the best time complexity for Count Zero Request Servers?
Optimal (sorted queries with a sliding window) runs in O(L log L + Q log Q + n) time and O(n + Q) extra space. Two sorts; then each log enters and leaves the window once, and each query is answered in O(1) after the pointers move.
Which pattern does Count Zero Request Servers use?
It is a arrays & hashing problem that uses the sliding window · hash map pattern. Other problems with the same pattern: Number of Substrings Containing All Three Characters, Minimum Window Substring, Longest Substring with At Most K Distinct Characters.
Is there a brute force solution for Count Zero Request Servers?
Yes. Scan all logs for each query takes O(Q · (L + n)) time and O(n) space. Answer each query on its own: walk through every log, and mark a server as busy the first time one of its requests falls inside [q - x, q].
Which edge cases should I test for Count Zero Request Servers?
A request exactly at q - x or exactly at q (both ends are inclusive); Several requests to the same server inside one window (it counts once); A window with no requests at all (answer n); Queries given out of order or repeated; Logs not sorted by time.