Pergunta

So my question is more algorithm based rather than code or architectural problem.

I have a problem where I am given an array of object like this: [{joinedAt: <Timestamp>, leftAt:<Timestamp> }]

now consider this scenario: I have 4 people having a meeting, each have its joinedAt and leftAt value.

I want to find max number of seats that were consumed during that meeting, considering 1 or more people can leave in between, also someone can walk out and comeback in and that would be a new entry every time a person walks in. so just because this array may have 5 values doesn't always mean they were 5 people but it can mean that someone went out and came back in later.

How can I devise an algorithm for such a problem.

The array is sorted with joinedAt value

Foi útil?

Solução

Change the array into a list of events

A joined at 12:00 - 1
B joined at 12:05 - 2
A left   at 12:06 - 1
B left   at 12:07 = 0

sort by time, loop through recording the total present at the time of each event. adding one when someone joins and removing one when someone leaves

Licenciado em: CC-BY-SA com atribuição
scroll top