Count Zero Request Servers

Medium Arrays & Hashing Sliding Window · Hash Map

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^5
  • 1 <= logs.length <= 10^5
  • 1 <= queries.length <= 10^5
  • logs[i].length == 2
  • 1 <= logs[i][0] <= n
  • 1 <= logs[i][1] <= 10^6
  • 1 <= x <= 10^5
  • x < 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.

00:00
Testcase

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.

ApproachTimeSpace
Scan all logs for each queryO(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

TimeO(Q · (L + n))Each of the Q queries scans all L logs and allocates a busy array of size n.
SpaceO(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.

  1. For each query q, start with count = n and a fresh busy array.
  2. For each log [s, t] with q - x <= t <= q: if s is not busy yet, mark it and decrease count.
  3. 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;
    }
}

2Optimal (sorted queries with a sliding window)

TimeO(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.
SpaceO(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.

  1. Sort logs by time. Sort the query indexes by their value.
  2. Keep cnt[server], active = 0, and pointers lo = hi = 0 into the logs.
  3. For each query q in increasing order: while logs[hi] has time <= q, add it (if cnt goes from 0 to 1, active++).
  4. While logs[lo] has time < q - x, remove it (if cnt drops to 0, active--).
  5. 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;
    }
}

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.