Feature #2: Show Busy Schedule

Implementing the "Show Busy Schedule" feature for our "Google Calendar" project.

Description

For the next feature, we want to find the times during which a user is busy. This feature is intended to show the busy hours of a user to other users without revealing the individual meeting slots. Therefore, if any meetings overlap or are back to back, then we want to merge their timings.

We will be provided a list of scheduled meetings, such as [[1, 4], [2, 5], [6, 8], [7, 9], [10, 13]]. In this schedule, [1, 4] and [2, 5], as well as [6, 8] and [7, 9], are overlapping. After merging these meetings, the schedule becomes [[1, 5], [6, 9], [10, 13]].

The illustration below shows a visual representation of the example.

Note: For simplicity, we are mapping the military timing to integers in the input. So, for example, 8:00 will be denoted by 8 in the code.

Solution

To solve this problem, it is best to sort the meetings based on the startTime. Then, we can determine if two meetings should be merged or not by processing them simultaneously.

Here is how we’ll implement this feature:

  • First, we will sort the meetings according to startTime.
  • Considering two meetings at a time, we will then check if the startTime of the second meeting is less than the endTime of the first meeting.
  • If the condition is true, merge the meetings into a new meeting and delete the existing ones.
  • Repeat the above steps until all the meetings are processed.
Press + to interact
use std::cmp;
fn merge_meetings( meeting_times:&mut Vec<Vec<i32>>, merged: &mut Vec<Vec<i32>>){
meeting_times.sort();
for meeting in meeting_times.into_iter(){
let size = merged.len();
if size == 0 || merged[size - 1][1] < meeting[0]{
merged.push(meeting.to_vec());
}
else{
merged[size - 1][1] = cmp::max(merged[size - 1][1], meeting[1]);
}
}
}
// Driver code
fn main() {
let mut meeting_times1: Vec<Vec<i32>> = vec![vec![1, 4], vec![2, 5], vec![6, 8], vec![7, 9], vec![10, 13]];
let mut merged: Vec<Vec<i32>>= Vec::new();
// First set of meetings
merge_meetings(&mut meeting_times1,&mut merged);
println!("{:?}",merged);
merged.clear();
// // Second set of meetings
let mut meeting_times2: Vec<Vec<i32>> = vec![vec![4, 7], vec![1, 3], vec![8, 10], vec![2, 3], vec![6, 8]];
merge_meetings(&mut meeting_times2,&mut merged);
println!("{:?}",merged);
}

Complexity measures

Time complexity Space complexity
O
...