Hash map · Easy
An ingestion API summarizes non-negative durations into fixed-width histogram buckets.
Return only occupied buckets sorted by bucket number.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ durations:integer[], width:integer }
Input rules
- Bucket index is floor(duration/width).
What each field means
durations— non-negative duration measurements.width— the size of each bucket. Durationdbelongs to bucketfloor(d / width).
Output
Return value and rules
- Return
{bucket, count}objects for occupied buckets only. - Order the objects by bucket number ascending.
- Empty measurements return
[].
Constraints
- 0 <= durations.length <= 100000; duration in [0,1000000000]
- 1 <= width <= 1000000
Examples
Example 1
{
"durations": [
0,
9,
10,
20
],
"width": 10
}Returns
[
{
"bucket": 0,
"count": 2
},
{
"bucket": 1,
"count": 1
},
{
"bucket": 2,
"count": 1
}
]Example 2
{
"durations": [],
"width": 1
}Returns
[]Required complexity
O(n + b log b) time and O(b) space