3851. Maximum Requests Without Violating the Limit π
Description
You are given a 2D integer array requests, where requests[i] = [useri, timei] indicates that useri made a request at timei.
You are also given two integers k and window.
A user violates the limit if there exists an integer t such that the user makes strictly more than k requests in the inclusive interval [t, t + window].
You may drop any number of requests.
Return an integer denoting the maximumβββββββ number of requests that can remain such that no user violates the limit.
Β
Example 1:
Input: requests = [[1,1],[2,1],[1,7],[2,8]], k = 1, window = 4
Output: 4
Explanation:βββββββ
- For user 1, the request times are
[1, 7]. The difference between them is 6, which is greater thanwindow = 4. - For user 2, the request times are
[1, 8]. The difference is 7, which is also greater thanwindow = 4. - No user makes more than
k = 1request within any inclusive interval of lengthwindow. Therefore, all 4 requests can remain.
Example 2:
Input: requests = [[1,2],[1,5],[1,2],[1,6]], k = 2, window = 5
Output: 2
Explanation:βββββββ
- For user 1, the request times are
[2, 2, 5, 6]. The inclusive interval[2, 7]of lengthwindow = 5contains all 4 requests. - Since 4 is strictly greater than
k = 2, at least 2 requests must be removed. - After removing any 2 requests, every inclusive interval of length
windowcontains at mostk = 2requests. - Therefore, the maximum number of requests that can remain is 2.
Example 3:
Input: requests = [[1,1],[2,5],[1,2],[3,9]], k = 1, window = 1
Output: 3
Explanation:
- For user 1, the request times are
[1, 2]. The difference is 1, which is equal towindow = 1. - The inclusive interval
[1, 2]contains both requests, so the count is 2, which exceedsk = 1. One request must be removed. - Users 2 and 3 each have only one request and do not violate the limit. Therefore, the maximum number of requests that can remain is 3.
Β
Constraints:
1 <= requests.length <= 105requests[i] = [useri, timei]1 <= k <= requests.length1 <= useri, timei, window <= 105
Solutions
Solution 1: Sliding Window + Deque
We can group the requests by user and store them in a hash table \(g\), where \(g[u]\) is the list of request times for user \(u\). For each user, we need to remove some requests from the request time list so that within any interval of length \(window\), the number of remaining requests does not exceed \(k\).
We initialize the answer \(\textit{ans}\) to the total number of requests.
For the request time list \(g[u]\) of user \(u\), we first sort it. Then, we use a deque \(kept\) to maintain the currently kept request times. We iterate through each request time \(t\) in the request time list. For each request time, we need to remove all request times from \(kept\) whose difference from \(t\) is greater than \(window\). Then, if the number of remaining requests in \(kept\) is less than \(k\), we add \(t\) to \(kept\); otherwise, we need to remove \(t\) and decrement the answer by 1.
Finally, return the answer \(\textit{ans}\).
The time complexity is \(O(n \log n)\) and the space complexity is \(O(n)\), where \(n\) is the number of requests. Each request is visited once, sorting takes \(O(n \log n)\) time, and the operations on the hash table and deque take \(O(n)\) time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |