Skip to content
Y
yTube.SITE v8.0.4
media / worlds / tools / internet oddities
YouTube / video information

LeetCode 2747 | Count Zero Request Servers | Java | Medium | Sliding Window & Sorting | CodeByTushu

Video information, playback and saved actions.

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
Β Β Β Β Β Β 
Videos related to "LeetCode 2747 | Count Zero Request Servers | Java | Medium | Sliding Window & Sorting | CodeByTushu"