Leetcode 2747: Count Zero Request Servers

grid47
grid47
Exploring patterns and algorithms
Feb 6, 2024 7 min read

Given a list of server requests and multiple queries specifying time intervals, determine the number of servers that did not receive any requests during each query’s time interval.
Problem
Approach
Steps
Complexity
Input: The input consists of the number of servers n, a 2D list of logs where each log contains the server id and time of request, an integer x for the time window, and a list of queries.
Example: n = 3, logs = [[1,3],[2,6],[1,5]], x = 5, queries = [10,11]
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
Output: Return a list of integers where each element represents the number of servers that did not receive any requests in the specified time interval for each query.
Example: [1, 2]
Constraints:
• The length of the result array should match the length of the queries array.
Goal: The goal is to compute the number of servers that did not receive any requests during the specified time intervals in each query.
Steps:
• Sort the logs based on the request time.
• For each query, calculate the relevant time interval and count how many servers did not receive requests during that time.
• Efficiently track which servers are active during each time window using a set and a map.
Goal: Constraints guide the limits on input sizes to ensure the solution is efficient.
Steps:
• 1 <= n <= 10^5
• 1 <= logs.length <= 10^5
• 1 <= queries.length <= 10^5
• 1 <= logs[i][1] <= 10^6
Assumptions:
• Each server can receive multiple requests.
• The logs are sorted by the request time.
Input: [3, [1, 3], [2, 6], [1, 5]], x = 5, queries = [10,11]
Explanation: This example explains how we can count servers that did not receive requests in the given time interval.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus