1193. Busiest Login Second

Medium3.5 points estimatedLine SweepSortingString

A server records user sessions as lines "HH:MM:SS user IN" and "HH:MM:SS user OUT", listed in any order. Nobody is logged in before the first event, and each user's events alternate IN, OUT, IN, OUT starting with IN, so a user may still be logged in at the end. A user's IN and OUT never fall in the same second.

Convert the times to seconds and process events in time order. When several events share a second, handle every OUT before any IN, so a person who leaves and another who arrives in the same second are never counted together. Return the largest number of users logged in at the same moment.

User names are lowercase letters. Sorting the events and sweeping once takes O(m log m) time and O(m) space for m log lines.

Example 1

Input:
logs = ["09:00:00 amy IN","09:30:00 bob IN","10:00:00 amy OUT","11:00:00 bob OUT"]
Output:
2
Explanation:

Between 09:30:00 and 10:00:00 both users are logged in, so the peak is 2.

Example 2

Input:
logs = ["10:00:00 amy OUT","10:00:00 bob IN","09:00:00 amy IN"]
Output:
1
Explanation:

At 10:00:00 the OUT is handled before the IN, so amy and bob are never logged in together.

Example 3

Input:
logs = ["00:00:05 cy IN","00:00:01 al IN","00:00:03 bo IN","00:00:09 al OUT","00:00:07 bo OUT"]
Output:
3
Explanation:

All three users are logged in from 00:00:05 until 00:00:07.

Constraints

1 ≤ logs.length ≤ 2000

user names are lowercase letters

times are valid and within one day

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(m log m)
Space
O(m)

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…