Queue Mean Queries
Problem statement
Process a sequence of operations on an initially empty first-in, first-out queue of integers.
PUSH xappends integerxto the back of the queue.POPremoves the oldest value from the front. EveryPOPoccurs when the queue is nonempty.MEANrequests the arithmetic mean of the values currently in the queue. Its result is0.0when the queue is empty.
Return one floating-point result for each MEAN, in request order. PUSH and POP produce no result entries. A mean is the current sum divided by the current number of values; it must not use integer division.
Support each operation in constant amortized time. Returned values are compared with relative-or-absolute tolerance 10^-9.
Function
queueMeans(operations: String[]) → double[]Examples
Example 1
operations = ["MEAN","PUSH 5","MEAN","PUSH 9","MEAN","POP","MEAN","POP","MEAN"]return = [0.0,5.0,7.0,9.0,0.0]The requested queue states are [], [5], [5,9], [9], and []. Their means are 0, 5, 7, 9, and 0.
Example 2
operations = ["PUSH -2","PUSH 3","PUSH 3","MEAN","POP","MEAN","PUSH 0","MEAN"]return = [1.3333333333333333,3.0,2.0]The first mean is (-2 + 3 + 3) / 3 = 4/3. Removing the oldest value leaves [3,3]. Appending 0 leaves [3,3,0].
Example 3
operations = ["PUSH 1","POP"]return = []No MEAN request occurs, so the returned array is empty.
Constraints
1 <= operations.length <= 2 * 10^5.- Each command is exactly
POP,MEAN, orPUSH xwith a signed decimal integerx. -10^9 <= x <= 10^9.- Every
POPremoves a value from a nonempty queue. - All commands are valid. Use a signed
64-bit running sum.