Event Statistics Aggregator
Problem statement
Process a finite sequence of operations for an event-statistics aggregator. Each event name keeps its count, sum, minimum, and maximum.
Every operation is a string array:
["add", name, value]records one decimal value fornameand produces no output.["count"]appends the total number of recorded events as a decimal integer string.["avg"],["min"], and["max"]append one formatted dictionary string for that statistic.
A formatted dictionary lists event names in ascending lexicographic order as name:value pairs separated by one space. Every numeric value has exactly six digits after the decimal point. A statistic query before any event is recorded appends the empty string.
Return the query outputs in operation order.
Function
aggregateEventStats(operations: String[][]) → String[]Examples
Example 1
operations = [["count"],["avg"],["add","click","2"],["add","purchase","10"],["add","click","4"],["count"],["avg"],["min"],["max"]]return = ["0","","3","click:3.000000 purchase:10.000000","click:2.000000 purchase:10.000000","click:4.000000 purchase:10.000000"]The first two queries observe the empty aggregator. After three additions, click has values 2 and 4, while purchase has value 10.
Example 2
operations = [["add","view","-1.5"],["add","view","2.5"],["avg"],["min"],["max"]]return = ["view:0.500000","view:-1.500000","view:2.500000"]The two view values average to 0.5; their minimum and maximum remain -1.5 and 2.5.
Constraints
1 <= operations.length <= 2000.- Every operation is one of the documented forms.
- Every event name is a non-empty string of lowercase English letters and underscores.
- Every added value is a base-10 decimal string in
[-10^6, 10^6]with at most three digits after the decimal point.