@@ -18,7 +18,7 @@ across all employees.
1818![ Example 2] ( ./images/examples/employee_free_time_example_2.png )
1919![ Example 3] ( ./images/examples/employee_free_time_example_3.png )
2020
21- ## Solution
21+ ## Solution 1
2222
2323The solution’s core revolves around merging overlapping intervals of employees and identifying the free time gaps
2424between these merged intervals. Using a min-heap, we arrange the intervals based on when they start, sorting them
@@ -66,3 +66,64 @@ complexity of processing the heap is O(mlog(n)).
6666
6767We use a heap in the solution, which can have a maximum of n elements. Hence, the space complexity of this solution is
6868O(n), where n is the number of employees.
69+
70+ ---
71+
72+ ## Solution 2
73+
74+ This problems builds upon the concept of merging intervals. We can solve this problem by first merging all the employee
75+ meeting intervals into a single list. The free times are then the gaps between those merged intervals.
76+
77+ > Important Note on Boundaries: In this problem, we only consider the gaps between busy intervals as free time. We do not consider:
78+ > 1 . Time before the earliest busy interval (e.g., if the first meeting starts at 9:00 AM, we don't count 8:00-9:00 AM as "free time")
79+ > 2 . Time after the latest busy interval (e.g., if the last meeting ends at 5:00 PM, we don't count 5:00-6:00 PM as "free time")
80+
81+ This is because the problem asks for common free time when all employees are available, and we're only given their
82+ scheduled busy intervals within a certain working timeframe.
83+
84+ ### Phase 1
85+
86+ We first want to flatten the list of intervals into a single list, and then sorting them by their start time to make the
87+ merge process easier.
88+
89+ ![ Solution 2.1] ( ./images/solutions/employee_free_time_2_solution_1.png )
90+ ![ Solution 2.2] ( ./images/solutions/employee_free_time_2_solution_2.png )
91+
92+ ### Phase 2
93+
94+ Next, we want to merge all the intervals into a single list. We can do this by iterating through the list of intervals
95+ and comparing the end time of the current interval with the start time of the next interval. If the end time of the
96+ current interval is greater than or equal to the start time of the next interval, we merge the two intervals. Otherwise,
97+ we add the current interval to the merged list.
98+
99+ ![ Solution 2.3] ( ./images/solutions/employee_free_time_2_solution_3.png )
100+ ![ Solution 2.4] ( ./images/solutions/employee_free_time_2_solution_4.png )
101+ ![ Solution 2.5] ( ./images/solutions/employee_free_time_2_solution_5.png )
102+ ![ Solution 2.6] ( ./images/solutions/employee_free_time_2_solution_6.png )
103+ ![ Solution 2.7] ( ./images/solutions/employee_free_time_2_solution_7.png )
104+ ![ Solution 2.8] ( ./images/solutions/employee_free_time_2_solution_8.png )
105+ ![ Solution 2.9] ( ./images/solutions/employee_free_time_2_solution_9.png )
106+ ![ Solution 2.10] ( ./images/solutions/employee_free_time_2_solution_10.png )
107+ ![ Solution 2.11] ( ./images/solutions/employee_free_time_2_solution_11.png )
108+
109+ ### Phase 3
110+
111+ In this phase, we return the employee free times by finding the gaps between the merged intervals. We can do this by
112+ iterating through the merged intervals, and creating a new interval from the end time of the current interval and the
113+ start time of the next interval.
114+
115+ ![ Solution 2.12] ( ./images/solutions/employee_free_time_2_solution_12.png )
116+ ![ Solution 2.13] ( ./images/solutions/employee_free_time_2_solution_13.png )
117+ ![ Solution 2.14] ( ./images/solutions/employee_free_time_2_solution_14.png )
118+ ![ Solution 2.15] ( ./images/solutions/employee_free_time_2_solution_15.png )
119+ ![ Solution 2.16] ( images/solutions/employee_free_time_2_solution_15.png )
120+
121+ ### Complexity Analysis
122+
123+ #### Time Complexity
124+
125+ O(n * logn) where n is the number of intervals. The time complexity is dominated by the sorting step.
126+
127+ #### Space Complexity
128+
129+ O(n) where n is the number of intervals. We need space for the free_times output array.
0 commit comments