Identify the Data Structure
You are given two arrays, operations and values, of equal length. Together, operations[i] and values[i] describe the i-th observed operation on an unknown data structure that starts empty.
- If operations[i] is "INSERT", values[i] is inserted into the structure.
- If operations[i] is "REMOVE", the next value removed from the structure was values[i].
The unknown structure may be one of the following:
- A stack, which removes the most recently inserted value.
- A FIFO queue, which removes the earliest inserted value still present.
- A max-priority queue, which removes the largest value currently present.
Simulate all three candidates independently. A candidate is permanently eliminated if a REMOVE operation is observed when it is empty, or if the value it would remove is different from values[i].
After processing every operation, return STACK, QUEUE, or MAX_PRIORITY_QUEUE if exactly one candidate remains valid. Return AMBIGUOUS if more than one candidate remains valid, and IMPOSSIBLE if no candidate remains valid.
Example 1:
Input: operations = ["INSERT", "INSERT", "REMOVE"], values = [1, 2, 1]
Output: QUEUE
Explanation: The stack and max-priority queue would both remove 2, while the FIFO queue removes 1. Therefore, only the queue remains valid.
Example 2:
Input: operations = ["INSERT", "INSERT", "REMOVE"], values = [1, 2, 2]
Output: AMBIGUOUS
Explanation: Both the stack and max-priority queue remove 2, while the FIFO queue would remove 1. Therefore, two candidates remain valid.
Now Your Turn!
Pick the correct output for the given inputInput: operations = ["INSERT", "REMOVE"], values = [5, 10]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= operations.length = values.length <= 105
- operations[i] is either "INSERT" or "REMOVE".
- -109 <= values[i] <= 109
- Duplicate values are allowed.