1343. Hourly Load Ranking

Easy1.5 points estimatedBucket SortArrayCountingSorting

A ticket desk records the time of every customer arrival as the number of seconds since midnight, stored in the array timestamps. Each value is in the range 0 to 86399, and the hour of an arrival is its timestamp divided by 3600, rounded down, so hours are numbered 0 to 23.

Group the arrivals by hour, then rank the hours by how many arrivals they hold. Return an array containing only the hours that have at least one arrival, ordered by arrival count from largest to smallest. When two hours have the same count, the smaller hour number comes first. Hours with no arrivals are left out. Aim for O(n) time using a 24-slot bucket array, since sorting only the busy hours costs a constant amount of extra work.

Example 1

Input:
timestamps = [3600,3700,7300,0,10000]
Output:
[1,2,0]
Explanation:

Hour 1 has 2 events, hour 2 has 2 events and hour 0 has 1 event. Hours 1 and 2 tie on count, so the smaller hour comes first.

Example 2

Input:
timestamps = [86399]
Output:
[23]
Explanation:

The single event falls in hour 23.

Example 3

Input:
timestamps = [0,1,2,7200]
Output:
[0,2]
Explanation:

Hour 0 has 3 events and hour 2 has 1 event.

Constraints

  • 1 ≤ n ≤ 1000, where n is the length of timestamps
  • 0 ≤ timestamps[i] < 86400

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…