Question
A web developer has set up a local web host on a low-power, single-board computer. The computer also hosts a media server that has a
A web developer has set up a local web host on a low-power, single-board computer. The computer also hosts a media server that has a higher priority. Each second time that the web host has priority, it responds to at most 5 requests and deletes them from the queue. Requests are served on a last in, first out basis. Given a list of times that the web host has priority, determine the total number of requests that are served.
Example: There are a total of n = 6 requests received
timestamp = [0, 1, 1, 2, 4, 5]
top = [5]
The web server has priority one time at top = 5. It can respond to the latest 5 requests through the 5th second of time. Those were received from timestamp[1] through timestamp[5]. The request from timestamp[0] is ignored. The total number of requests served is 5.
Note:
The server may have priority multiple times in a second.
The arrays timestamp and top may not be in sorted order.
Function Description:
Complete the function requestsServed in the editor below. The function should return an integer that denotes the total number requests served. requestsServed has the following parameters:
timestamp[timestamp[0],...timestamp[n-1]]: An array of integers that represent the seconds after time = 0 that the requests arrive
top[top[0]...top[m-1]]: An array of integers that represent the seconds after time = 0 that the server has priority
Constraints:
1 ≤ n ≤ 105
1 ≤ m ≤ 30
0 ≤ timestamp[i] < 60
0 ≤ top[j] < 60
Input Format For Custom Testing
The first line contains an integer n, the number of requests received. Each of the next n lines contains an integer, timestamp[i], the time in seconds after time = 0 the ith request was received. The next line contains an integer m, the number of times the server will have priority. Each of the next m linescontains an integer, top[j], the jth time in seconds after time = 0 the server will have priority.
Sample Case 0
Sample Input For Custom Testing
10 1 2 2 3 4 5 6 6 13 16 2 10 15
Sample Output
9
Explanation
n = 10
timestamp = [1, 2, 2, 3, 4, 5, 6, 6, 13, 16]
top = [10, 15]
The server receives n = 10 requests and has priority m = 2 times at top = [10, 15] seconds after time = 0. The first time requests are served, at the 10th second, the 5 requests received at [3, 4, 5, 6, 6] are served and they are deleted from the queue. Now timestamp' = [1, 2, 2, 13, 16]. The next time the server has priority, in the 15th second, it serves the 4 requests received at [1, 2, 2, 13] respectively and deletes them as well. A total of 9 requests were served. Since the server did not have priority after time = 15, the request received at time = 16 is not served.
Sample Case 1
Sample Input For Custom Testing
7 2 2 4 8 11 28 30 4 0 5 5 15
Sample Output
5
Explanation
n = 7 timestamp = [2, 2, 4, 8, 11, 28, 30] top = [0, 5, 5, 15] The first time the server has priority in the 0th second, there are no pending requests, so none is served. The second time, in the 5th second, the 3 requests received at [2, 2, 4] are served. The third time, in the 5th second, there are no pending requests, so none is served. The fourth time, in the 15th second, the 2 requests received at [8, 11] are served. A total of 5 requests were served.
Sample Case 2
Sample Input For Custom Testing
17 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 6 6 6 6
Sample Output
17
Explanation
n = 17 timestamp = [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] top = [6, 6, 6, 6] The server has priority in the 6th second, the latest 5 of the requests received in the 1st second are served. The second time, in the 6th second, the next 5 of the requests received in the 1st second are served. The third time, in the 6th second, the next 5 of the requests received in the 1st second are served. The fourth time, again in the 6th second, the 2 remaining requests received in the 0th second are served. A total of 17 requests were served.
public static int requestsServed(List timestamp, List top) {
// Write your code here, please.
Step by Step Solution
3.37 Rating (150 Votes )
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started