...

/

Feature #7: Longest Busy Period

Feature #7: Longest Busy Period

Implementing the "Longest Busy Period" feature for our "Google Calendar" project.

Description

A person has divided their workday into 15-minute time slots numbered as 1, 2, 3, ... n. People who want to schedule a meeting with this person choose any available time slot that suits them. Assume that this person’s calendar is now filled with jumbled up numbers that represent the time slots reserved for meetings. Your task is to find out what the longest consecutive time period in which this person will be busy is.

The following illustration shows how we will be mapping the 15-minute time slots to the numbers given in the input:

For example, you are given the following list of busy slots: [3, 1, 15, 5, 2, 12, 10, 4, 8, 9]. In this list, the longest sequence of consecutive slots is [1, 2, 3, 4, 5]. Since the length of this sequence is five, your function should return 5.

Solution

The longest busy period can start from any of the array’s indexes, so we have to search the entire array. Additionally, for each slot, we need to look up the entire array for the next consecutive slot. We can use the set data structure and convert a linear time lookup to constant time to find the longest sequence of consecutive numbers.

Here is how the implementation will take place:

  1. Initialize a set data structure and store the busy schedule in it.

  2. Traverse the input array, and for each time slot, check if the next consecutive slot is in the array or not.

  3. If the above condition is true, keep incrementing the slot number until we get a slot that is not in the set.

  4. We will keep the count of consecutive slots in a ...

Access this course and 1400+ top-rated courses and projects.