Skip to content

Latest commit

 

History

History
38 lines (24 loc) · 2.18 KB

File metadata and controls

38 lines (24 loc) · 2.18 KB

Problem: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings.

Remember that [[1, 4],[4, 7]] will not conflict at 4.

Examples: Input: intervals = [[1,3],[3,7],[6,8],[8, 10], [10, 12]] Output: false Explanation: The meeting intervals [3,7], [6,8] will conflict

Input: intervals = [[1,5], [5,7], [8, 10]] Output: true Explanation: The three meeting intervals will not conflict

Algorithmic Steps This problem is solved with the help of greedy approach. The algorithmic approach can be summarized as follows:

  1. First, sort the list of intervals based on their start times. This step helps to keep the intervals arranged in a sequential order and makes it easy to check for any overlaps in the list.

  2. Iterate through the list of intervals. For each iteration, keep the current start time(i.e, startTime) and previous end time(i.e, prevEndTime) values to compare start time with previous meeting's end time.

    Note: Remember that the main idea behind this solution is to verify that each meeting ends before the next meeting starts.

  3. If the start time is less than the end time of the previous interval, then there is an overlap between the intervals. In this case, a person cannot attend all meetings. So you need to return false immediately.

  4. If the start time is greater than the previous interval's end time, then there is no overlap between the intervals. In this case, you need to proceed with next set of intervals to find any overlap between the meetings.

  5. Repeat steps 2-4 until there is no overlap exists in the list or all intervals have been traversed.

  6. Return true indicating that a person can attend all meetings.

Time and Space complexity: This algorithm has a time complexity of O(n logn) + O(n) ~= O(n), where n is the number of intervals. This is because sort operation typically takes a time complexity of O(n log n) for sorting the list. Thereafter, iteration over the intervals take a time complexity of O(n). So the total time complexity simplifies to O(n log n).

The space complexity of this algorithm is O(1) due to no additional data structure used.