1702. Lowest In Line
A service desk serves people strictly in the order they arrive, and the supervisor wants to know the smallest ticket number waiting at any moment. Design a class MinLine whose constructor takes no arguments and which supports three methods.
push(x) adds the integer x to the back of the line and returns nothing. pop() removes the element at the front of the line and returns it, or returns -1 if the line is empty. getMin() returns the smallest value currently in the line without removing anything, or -1 if the line is empty. Values are never negative, so -1 is unambiguous; duplicates are allowed and every copy is a separate entry. Every method must run in amortised O(1) time, so scanning the whole line on each getMin is not acceptable. Keep a second deque of minimum candidates beside the main queue. Space is O(n).
Example 1
- Input:
- operations = ["MinLine","push","push","getMin","pop","getMin","pop","getMin"]arguments = [[],[5],[3],[],[],[],[],[]]
- Output:
- [null,null,null,3,5,3,3,-1]
- Explanation:
After pushing 5 and 3 the minimum is 3; pop returns the front 5 and the minimum stays 3; the next pop returns 3 and the empty line reports -1.
Example 2
- Input:
- operations = ["MinLine","pop","getMin","push","getMin","pop","pop"]arguments = [[],[],[],[7],[],[],[]]
- Output:
- [null,-1,-1,null,7,7,-1]
- Explanation:
On an empty line pop and getMin both return -1; after push(7) the minimum is 7, one pop returns it and the next pop returns -1 again.
Example 3
- Input:
- operations = ["MinLine","push","push","push","pop","getMin","pop","getMin","pop","getMin"]arguments = [[],[2],[2],[2],[],[],[],[],[],[]]
- Output:
- [null,null,null,null,2,2,2,2,2,-1]
- Explanation:
Three equal values are three separate entries: each pop returns 2 and the minimum stays 2 until the line is empty.
Constraints
0 ≤ x ≤ 109
At most 1000 calls in total
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Input
- Each case is an operation log.
operationsnames the class first and then each method call;argumentsholds the arguments for each, in the same order. Your answer is one list with a result per operation -nullfor the constructor and for methods that return nothing.
Expected complexity
- Time
- O(1) amortised per call
- Space
- O(n)