--- Difficulty: Easy Source: 160 Days of Problem Solving (BONUS PROBLEMS 🎁) Tags: - Sorting - Greedy - Arrays --- # πŸš€ _2. Meeting Rooms_ 🧠 The problem can be found at the following link: [Problem Link](https://www.geeksforgeeks.org/batch/gfg-160-problems/track/sorting-bonus-problems/problem/attend-all-meetings) ## πŸ’‘ **Problem Description:** Given an array `arr[][]`, where `arr[i][0]` is the starting time of the i-th meeting and `arr[i][1]` is the ending time of the i-th meeting, determine if it is possible for a person to attend all the meetings. A person can only attend one meeting at a time, meaning each meeting must start after the previous one ends. ## πŸ” **Example Walkthrough:** **Input:** ``` arr[][] = [[1, 4], [10, 15], [7, 10]] ``` **Output:** ``` true ``` **Explanation:** Since all the meetings are held at different times, it is possible to attend all the meetings. **Input:** ``` arr[][] = [[2, 4], [9, 12], [6, 10]] ``` **Output:** ``` false ``` **Explanation:** The second and third meetings overlap, making it impossible to attend all of them. ## 🎯 **My Approach:** 1. **Sorting and Overlap Checking:** - First, sort the meetings based on their starting times. - Traverse through the sorted list, comparing the end time of the current meeting with the start time of the next meeting. - If any meeting starts before the previous one ends, return `false` as overlapping prevents attending all meetings. 2. **Edge Cases:** - If there are no meetings, return `true`. - If there is only one meeting, return `true` as no overlap can occur. ## πŸ•’ **Time and Auxiliary Space Complexity:** - **Expected Time Complexity:** O(n log n), where `n` is the number of meetings. Sorting the list of meetings takes O(n log n), while checking overlaps requires O(n). - **Expected Auxiliary Space Complexity:** O(1), as we only use a constant amount of additional space to store indices and temporary values. ## πŸ“ **Solution Code** ## Code (C++) ```cpp class Solution { public: bool canAttend(vector>& arr) { int n = arr.size(); sort(arr.begin(), arr.end()); for (int i = 0; i < n - 1; i++) { if (arr[i][1] > arr[i + 1][0]) return false; } return true; } }; ```

πŸ‘¨β€πŸ’» Alternative Approaches

### Alternative Approach (Using Lambda Comparator) ```cpp class Solution { public: bool canAttend(std::vector>& intervals) { std::sort(intervals.begin(), intervals.end(), [](const auto& a, const auto& b) { return a[0] < b[0]; }); for (int i = 1; i < intervals.size(); ++i) { if (intervals[i - 1][1] > intervals[i][0]) { return false; } } return true; } }; ```
## Code (Java) ```java class Solution { static boolean canAttend(int[][] arr) { Arrays.sort(arr, (a, b) -> Integer.compare(a[0], b[0])); for (int i = 0; i < arr.length - 1; i++) { if (arr[i][1] > arr[i + 1][0]) { return false; } } return true; } } ``` ## Code (Python) ```python class Solution: def canAttend(self, arr): arr.sort(key=lambda x: x[0]) for i in range(len(arr) - 1): if arr[i][1] > arr[i + 1][0]: return False return True ``` ## 🎯 Contribution and Support: For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: [Any Questions](https://www.linkedin.com/in/patel-hetkumar-sandipbhai-8b110525a/). Let’s make this learning journey more collaborative! ⭐ If you find this helpful, please give this repository a star! ⭐ ---

πŸ“Visitor Count