All problems

Parcel Lockers

Medium · heap

A building has n parcel lockers, numbered 1 to n, and all of them start empty. Events happen in the order given. For event k, actions[k] is either "deliver" or "collect", and ids[k] is the number of the parcel involved.

Return a list with one number for every "deliver" event, in order: the number of the locker the parcel was put in, or -1 if it was turned away. A parcel number can be delivered again after it has been collected or turned away.

Examples

Example 1

Input: n = 2, actions = ["deliver", "deliver", "deliver", "collect", "deliver"], ids = [10, 11, 12, 10, 13]

Output: [1, 2, -1, 1]

Parcels 10 and 11 fill lockers 1 and 2. Parcel 12 is turned away. Collecting parcel 10 frees locker 1, which parcel 13 then takes.

Example 2

Input: n = 3, actions = ["deliver", "deliver", "collect", "deliver", "collect", "deliver"], ids = [5, 6, 5, 7, 6, 8]

Output: [1, 2, 1, 2]

Locker 1 is freed and reused by parcel 7. When parcel 6 leaves locker 2, parcel 8 takes it.

Example 3

Input: n = 1, actions = ["collect", "deliver", "deliver"], ids = [4, 4, 9]

Output: [1, -1]

Collecting a parcel that was never stored does nothing. Parcel 4 takes the only locker, so parcel 9 is turned away.

Constraints