Count Zero Request Servers - Complete Solution Guide
Count Zero Request Servers is LeetCode problem 2747, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an integer n denoting the total number of servers and a 2D 0-indexed integer array logs , where logs[i] = [server_id, time] denotes that the server with id server_id received a request at time time . You are also given an integer x and a 0-indexed integer array queries . Return a 0-indexed integer array arr of length queries.length where arr[i] represents the number of servers that did not receive any requests during the time interval [queries[i] - x, queries[i]] . Note that the ti
Detailed Explanation
The problem asks us to determine, for each query in a given list of queries, how many servers out of a total of 'n' servers did *not* receive any requests within a specific time window. We are given 'logs', a 2D array where each row represents a request made to a server at a specific time. Each query consists of a time 'q_time', and the time window for that query is defined as [q_time - x, q_time] inclusive. We need to return an array of the same length as the queries array, where each element represents the count of servers that had no requests during the corresponding query's time window. The input consists of the total number of servers 'n', the logs of server requests 'logs', the window size 'x', and the query times 'queries'. The output is an array of integers where each integer represents the number of idle servers for the corresponding query.
Solution Approach
The solution utilizes a sliding window approach combined with sorting and a hash map. First, the `logs` array is sorted by timestamp, and the `queries` array is sorted by query time along with their original indices (to maintain the correct output order). Then, for each query, a window is maintained over the `logs` array. The right edge of the window is expanded to include all logs with timestamps within the range [query_time - x, query_time]. The left edge of the window is contracted to exclude logs with timestamps less than query_time - x. A hash map tracks the count of requests received by each server within the current window. The number of servers that did not receive any requests is then calculated as 'n' minus the number of distinct servers in the hash map.
Step-by-Step Algorithm
- Step 1: Sort the `logs` array by the timestamp (the second element in each log entry) in ascending order.
- Step 2: Create a new data structure (e.g., an array of pairs) to store each query along with its original index. Sort this new data structure by the query time.
- Step 3: Initialize an empty hash map (e.g., `server_counts`) to store the count of requests for each server within the current time window.
- Step 4: Initialize two pointers, `left_log_idx` and `right_log_idx`, to 0. These pointers define the left and right boundaries of the sliding window over the sorted `logs` array.
- Step 5: Iterate through the sorted queries along with their original indices.
- Step 6: For each query, calculate the `start_time` as `query_time - x`.
- Step 7: Expand the right edge of the window: While `right_log_idx` is within the bounds of the `logs` array and the timestamp of the log entry at `right_log_idx` is less than or equal to `query_time`, increment the count of the server in the `server_counts` hash map. Increment `right_log_idx`.
- Step 8: Contract the left edge of the window: While `left_log_idx` is less than `right_log_idx` and the timestamp of the log entry at `left_log_idx` is less than `start_time`, decrement the count of the server in the `server_counts` hash map. If the count becomes 0, remove the server from the hash map. Increment `left_log_idx`.
- Step 9: Calculate the number of servers that did not receive any requests during the current query's time window as `n - len(server_counts)`, where `len(server_counts)` is the number of distinct servers in the hash map. Store this result in the `ans` array at the original index of the query.
- Step 10: Return the `ans` array.
Key Insights
- Insight 1: Sorting the logs by timestamp and the queries by query time allows us to efficiently use a sliding window approach.
- Insight 2: Using a hash map (or similar data structure) to keep track of which servers received requests during the current window enables fast counting of active servers.
- Insight 3: The core idea is to slide a window over the sorted logs, incrementing and decrementing server request counts in the hash map as we move the window boundaries to match the current query.
Complexity Analysis
Time Complexity: O(m log m + q log q + m)
Space Complexity: O(m + q)
Topics
This problem involves: Array, Hash Table, Sliding Window, Sorting.
Companies
Asked at: DP world, Flexport, Honeywell, LTI, Zomato.