A drone-relay network shares a set of integer frequency channels among its fleet. Any number of drones may broadcast on the same frequency at once. You are given a log of Q commands, each describing a drone joining a frequency, a leave being requested, or a status check, and must report the outcome of every check and leave.
Each command is one of:
ADD x -- a drone begins broadcasting on frequency x. Frequency x may already have other drones on it; this simply adds one more.SEARCH x -- report whether at least one drone is currently broadcasting on frequency x.REMOVE x -- take exactly one drone off frequency x, if any drone is currently on it (this removes only a single occurrence, leaving any others on that frequency untouched). Report whether a drone was actually removed.The first line contains a single integer Q. Each of the next Q lines contains one command in one of the three forms above, with x a non-negative integer.
For every SEARCH and REMOVE command, print YES or NO on its own line, in the order the commands appear: YES if the frequency currently has at least one drone on it (SEARCH) or a drone was actually removed (REMOVE), and NO otherwise. Print nothing for ADD commands.
ADD, SEARCH, or REMOVE in any order and any mix.Example 1
Input
5 ADD 5 SEARCH 5 ADD 5 REMOVE 5 SEARCH 5
Expected
YES YES YES
Explanation
ADD 5 puts one drone on frequency 5. SEARCH 5 finds a drone there, so it prints YES. ADD 5 adds a second drone to frequency 5. REMOVE 5 takes one drone off (one remains), printing YES. SEARCH 5 still finds the remaining drone, printing YES.
Example 2
Input
4 SEARCH 3 REMOVE 3 ADD 3 SEARCH 3
Expected
NO NO YES
Explanation
SEARCH 3 finds no drone on frequency 3 yet, printing NO. REMOVE 3 has nothing to remove, printing NO. ADD 3 puts a drone on frequency 3. SEARCH 3 now finds it, printing YES.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →