Messaging
Rookie SRM 16 · 2022-08-22 · by lars2520
Problem Statement
Your task is to model this messaging service. In addition to messages, you will be given an int, time, representing the number of seconds since the system was started. Your task is to determine which message the service sends at that second and return a
Notes
- Note that, to make things simpler, each priority is distinct.
Constraints
- messages will contain between 0 and 50 elements, inclusive.
- Each element of messages will contain between 5 and 50 characters, inclusive.
- Each element of messages will be formatted as "<message> <priority> <time>".
- <message> will be a sequence of 1 or more lowercase letters ('a'-'z').
- <priority> will be an integer with no leading zeros, between 1 and 100, inclusive.
- Each <priority> will be distinct.
- <time> will be an integer with no extra leading zeros between 0 and 1000, inclusive.
- time will be between 0 and 2000, inclusive.
- Elements of messages will be sorted in non-decreasing order, by time.
{"hello 1 1","how 4 1","are 2 3","you 5 4"}
1
Returns: "how"
At time = 1, both "hello" and "how" are added to the queue. Of these, "how" has the higher priority, so it is sent.
{"hello 1 1","how 4 1","are 2 3","you 5 4"}
2
Returns: "hello"
At time = 2, no new messages arrive, so the remaining message from time 1 is sent.
{"hello 1 1","how 4 1","are 2 3","you 5 4"}
3
Returns: "are"
At time = 3, the queue starts out empty, and then receives a single message, "are". Since there is only one message in the queue to send, it is sent on.
{"hello 1 1","how 4 1","are 2 3","you 5 5"}
4
Returns: ""
At time = 4, the queue is empty.
{"priority 9 1",
"be 3 1",
"queues 5 2",
"can 4 3",
"implemented 1 3",
"but 8 7",
"in 11 7",
"would 2 7",
"nlogn 12 8",
"yours 21 10",
"that 10 10",
"slower 18 10",
"than 14 11",
"can 75 11",
"be 50 11",
"much 42 13",
"be 30 30"}
14
Returns: "slower"
Here is what the queue would send at each time step. (Note the hint and don't try to implement a heap) Time | Message sent ('_' means no message was sent) -----+-------------- 0 | _ 1 | priority 2 | queues 3 | can 4 | be 5 | implemented 6 | _ 7 | in 8 | nlgn 9 | but 10 | yours 11 | can 12 | be 13 | much 14 | slower 15 | that 16 | than 17 | would 18 | _ ... | _ 30 | be
Submissions are judged against all 73 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class Messaging with a public method string getNext(vector<string> messages, int time) · 73 test cases · 2 s / 256 MB per case