Oyo Room Assignment Log
At Oyo, efficiently managing room assignments is crucial for smooth hotel operations and guest satisfaction. As part of a new internal tool, we need to process a historical log of room assignment changes to determine the final state of assigned rooms. This problem simulates processing such a log, which involves adding assignments, removing the most recent ones, swapping recent assignments, and batch-unassigning multiple rooms.
Implement a function processAssignmentLog that takes an array of strings representing room assignment operations and returns the final ordered list of assigned rooms. The operations are processed sequentially.
The operations can be one of the following:
K most recently assigned rooms should be freed up. K will be an integer embedded in the string (e.g., "BATCH_UNASSIGN_3" means unassign the 3 most recent rooms). If there are fewer than K assigned rooms, all currently assigned rooms are freed up.The final list of assigned rooms should be returned in the order they were originally assigned, from oldest to newest.
operations: string[] - An array of strings, where each string represents an operation as described above.string[] - An array of strings representing the final list of assigned rooms, ordered from oldest to newest.K after the underscore. K will be a positive integer.Examples
Example 1
Input:
operations = ["101", "201", "UNASSIGN", "301", "SWAP"]
Output:
["301", "101"]
Note:
[] -> ["101"] (Assign 101)["101"] -> ["101", "201"] (Assign 201)["101", "201"] -> ["101"] (Unassign 201)["101"] -> ["101", "301"] (Assign 301)["101", "301"] -> ["301", "101"] (Swap 101 and 301)
Final assigned rooms: ["301", "101"].Example 2
Input:
operations = ["VIP", "SUITE", "105", "BATCH_UNASSIGN_2", "200", "UNASSIGN", "SWAP", "UNASSIGN"]
Output:
[]
Note:
[] -> ["VIP"] (Assign VIP)["VIP"] -> ["VIP", "SUITE"] (Assign SUITE)["VIP", "SUITE"] -> ["VIP", "SUITE", "105"] (Assign 105)["VIP", "SUITE", "105"] -> ["VIP"] (BATCH_UNASSIGN_2: removes 105, SUITE)["VIP"] -> ["VIP", "200"] (Assign 200)["VIP", "200"] -> ["VIP"] (UNASSIGN: removes 200)["VIP"] -> ["VIP"] (SWAP: only one room, no change)["VIP"] -> [] (UNASSIGN: removes VIP)
Final assigned rooms: [].Example 3
Input:
operations = ["R1", "R2", "R3", "R4", "BATCH_UNASSIGN_1", "BATCH_UNASSIGN_5", "R5"]
Output:
["R5"]
Note:
[] -> ["R1"]["R1"] -> ["R1", "R2"]["R1", "R2"] -> ["R1", "R2", "R3"]["R1", "R2", "R3"] -> ["R1", "R2", "R3", "R4"]["R1", "R2", "R3", "R4"] -> ["R1", "R2", "R3"] (BATCH_UNASSIGN_1: removes R4)["R1", "R2", "R3"] -> [] (BATCH_UNASSIGN_5: removes all 3 rooms, since 3 < 5)[] -> ["R5"]
Final assigned rooms: ["R5"].Constraints
1 <= operations.length <= 10^5operations will have a length between 1 and 10 characters.K in "BATCH_UNASSIGN_K" will be an integer 1 <= K <= 10^5.10^5.10^5 elements.Loading editor...