LeetCode 2747 | Count Zero Request Servers | Java | Medium | Sliding Window & Sorting | CodeByTushu
Description: π Welcome Back to CodeByTushu!
In todayβs video, we solve LeetCode 2747 β Count Zero Request Servers using Offline Queries, Sliding Window & Two Pointers in Java.
In this problem, we are given an integer n denoting the total number of servers, a 2D log array where each entry records a server ID and timestamp, an integer window size x, and a list of query end times. For each query time q, we must find how many of the n servers received zero requests in the inclusive time window from q - x to q. A naive scan for each query would lead to Time Limit Exceeded due to large constraints.
We solve this with an optimal monotonic offline sliding window approach. By sorting both the logs by timestamp and queries by their query times in ascending order while preserving their original indices, we can advance a two-pointer sliding window linearly across the sorted event logs. As the query window moves forward, the right pointer ingests all server requests with timestamps up to q, and the left pointer evicts expired requests with timestamps strictly before q - x. Using a server request frequency table, we track unique active servers in constant time and compute the zero-request server count as n minus active servers, achieving optimal O(M log M + Q log Q) time complexity and O(N + Q) auxiliary space.
π₯ Problem Highlights
β Offline query processing: sorting queries chronologically decouples per-query recomputations
β Monotonic sliding window: right pointer ingests incoming logs and left pointer evicts expired logs
β Constant-time state tracking: frequency array maintains the count of unique active servers
β Handles concurrent server requests and overlapping time intervals seamlessly
β Zero redundant work: each log event is processed into and out of the active window at most once
β Optimal O(M log M + Q log Q) time complexity with O(N + Q) auxiliary space
β±οΈ TIMESTAMPS
00:00 Intro & Day 104 Streak Celebration
00:33 Problem Understanding & Constraints Analysis
01:41 Key Breakthrough: Monotonic Offline Sliding Window
02:27 5-Step Pipeline: Sorting & Two Pointers Ingest/Evict
03:33 Optimal Java 17 Solution Walkthrough Line by Line
04:39 Test Cases Walkthrough & Day 104 Streak Celebration
05:14 Time Complexity Analysis (O(M log M + Q log Q))
05:59 Space Complexity Analysis (O(N + Q) Auxiliary Space)
06:24 Summary & Outro!
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/November/06-November/2747-Count-Zero-Request-Servers
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/06-November/2747-Count-Zero-Request-Servers/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/06-November/2747-Count-Zero-Request-Servers/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/06-November/2747-Count-Zero-Request-Servers/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/06-November/2747-Count-Zero-Request-Servers/solution.js
π LeetCode Source Code
π https://codebytushu.com/leetcode/problem/count-zero-request-servers
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Medium Problems
Java DSA Preparation
Offline Query Processing
Sliding Window & Two Pointers
Frequency Array Optimization
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why does sorting the queries chronologically allow the two-pointer sliding window to process all log events in O(M) total amortized time?
1οΈβ£ Both window boundaries only move forward monotonically across the sorted logs
2οΈβ£ It converts the problem into binary search over logs
3οΈβ£ It removes duplicate servers automatically
4οΈβ£ It reduces the window size to a constant 1
#LeetCode2747 #LeetCode #CountZeroRequestServers #Java #JavaDSA #SlidingWindow #TwoPointers #Array #Sorting #OfflineQueries #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
In todayβs video, we solve LeetCode 2747 β Count Zero Request Servers using Offline Queries, Sliding Window & Two Pointers in Java.
In this problem, we are given an integer n denoting the total number of servers, a 2D log array where each entry records a server ID and timestamp, an integer window size x, and a list of query end times. For each query time q, we must find how many of the n servers received zero requests in the inclusive time window from q - x to q. A naive scan for each query would lead to Time Limit Exceeded due to large constraints.
We solve this with an optimal monotonic offline sliding window approach. By sorting both the logs by timestamp and queries by their query times in ascending order while preserving their original indices, we can advance a two-pointer sliding window linearly across the sorted event logs. As the query window moves forward, the right pointer ingests all server requests with timestamps up to q, and the left pointer evicts expired requests with timestamps strictly before q - x. Using a server request frequency table, we track unique active servers in constant time and compute the zero-request server count as n minus active servers, achieving optimal O(M log M + Q log Q) time complexity and O(N + Q) auxiliary space.
π₯ Problem Highlights
β Offline query processing: sorting queries chronologically decouples per-query recomputations
β Monotonic sliding window: right pointer ingests incoming logs and left pointer evicts expired logs
β Constant-time state tracking: frequency array maintains the count of unique active servers
β Handles concurrent server requests and overlapping time intervals seamlessly
β Zero redundant work: each log event is processed into and out of the active window at most once
β Optimal O(M log M + Q log Q) time complexity with O(N + Q) auxiliary space
β±οΈ TIMESTAMPS
00:00 Intro & Day 104 Streak Celebration
00:33 Problem Understanding & Constraints Analysis
01:41 Key Breakthrough: Monotonic Offline Sliding Window
02:27 5-Step Pipeline: Sorting & Two Pointers Ingest/Evict
03:33 Optimal Java 17 Solution Walkthrough Line by Line
04:39 Test Cases Walkthrough & Day 104 Streak Celebration
05:14 Time Complexity Analysis (O(M log M + Q log Q))
05:59 Space Complexity Analysis (O(N + Q) Auxiliary Space)
06:24 Summary & Outro!
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/November/06-November/2747-Count-Zero-Request-Servers
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/06-November/2747-Count-Zero-Request-Servers/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/06-November/2747-Count-Zero-Request-Servers/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/06-November/2747-Count-Zero-Request-Servers/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/06-November/2747-Count-Zero-Request-Servers/solution.js
π LeetCode Source Code
π https://codebytushu.com/leetcode/problem/count-zero-request-servers
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Medium Problems
Java DSA Preparation
Offline Query Processing
Sliding Window & Two Pointers
Frequency Array Optimization
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why does sorting the queries chronologically allow the two-pointer sliding window to process all log events in O(M) total amortized time?
1οΈβ£ Both window boundaries only move forward monotonically across the sorted logs
2οΈβ£ It converts the problem into binary search over logs
3οΈβ£ It removes duplicate servers automatically
4οΈβ£ It reduces the window size to a constant 1
#LeetCode2747 #LeetCode #CountZeroRequestServers #Java #JavaDSA #SlidingWindow #TwoPointers #Array #Sorting #OfflineQueries #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description




