LeetCode 2747: Count Zero Request Servers Solution

Master LeetCode problem 2747 (Count Zero Request Servers), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2747. Count Zero Request Servers

Problem Explanation

Explanation

To solve this problem, we can use a sliding window technique. We will maintain a set to keep track of servers that have received a request within the last x time units. We will iterate through the logs and update this set accordingly. Then, for each query, we will iterate through all servers and count the servers that are not in the set for the given time interval.

  • We will start by initializing an empty set and an array lastRequest to keep track of the last request time for each server.
  • For each log entry, we will update the lastRequest array and add the server to the set if the request time is within the interval [queries[i] - x, queries[i]].
  • For each query, we will iterate through all servers and count the servers that are not in the set for the given time interval.

Time complexity: O(n + q), where n is the number of logs and q is the number of queries. Space complexity: O(n), where n is the number of servers.

Solution Code

class Solution {
    public int[] countServers(int n, int[][] logs, int x, int[] queries) {
        Set<Integer> activeServers = new HashSet<>();
        int[] lastRequest = new int[n];
        int[] result = new int[queries.length];
        
        for (int[] log : logs) {
            int serverId = log[0] - 1;
            int time = log[1];
            lastRequest[serverId] = time;
            if (time >= queries[0] - x && time <= queries[0]) {
                activeServers.add(serverId);
            }
        }
        
        for (int i = 0; i < queries.length; i++) {
            int startTime = queries[i] - x;
            int endTime = queries[i];
            for (int j = 0; j < n; j++) {
                if (lastRequest[j] < startTime || lastRequest[j] > endTime) {
                    result[i]++;
                }
            }
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2747 (Count Zero Request Servers)?

This page provides optimized solutions for LeetCode problem 2747 (Count Zero Request Servers) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2747 (Count Zero Request Servers)?

The time complexity for LeetCode 2747 (Count Zero Request Servers) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2747 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2747 in Java, C++, or Python.

Back to LeetCode Solutions