|
| 1 | +# Meeting Rooms |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +Given an array of meeting time intervals `intervals` where `intervals[i] = [starti, endi]`, determine if a person could attend all meetings. |
| 6 | + |
| 7 | +## Examples |
| 8 | + |
| 9 | +**Example 1:** |
| 10 | +``` |
| 11 | +Input: intervals = [[0,30],[5,10],[15,20]] |
| 12 | +Output: false |
| 13 | +``` |
| 14 | + |
| 15 | +## Approach |
| 16 | + |
| 17 | +### Method 1: Sorting + Check (Recommended) |
| 18 | +1. Sort intervals by start time |
| 19 | +2. Check if any interval overlaps with the next |
| 20 | +3. Most efficient approach |
| 21 | + |
| 22 | +**Time Complexity:** O(n log n) - Sorting |
| 23 | +**Space Complexity:** O(1) - In-place modification |
| 24 | + |
| 25 | +### Method 2: Brute Force |
| 26 | +1. Check all pairs of intervals for overlap |
| 27 | +2. Less efficient than sorting approach |
| 28 | + |
| 29 | +**Time Complexity:** O(n²) - Nested loops |
| 30 | +**Space Complexity:** O(1) - No extra space |
| 31 | + |
| 32 | +## Algorithm |
| 33 | + |
| 34 | +``` |
| 35 | +1. Sort intervals by start time |
| 36 | +2. For i from 0 to n-2: |
| 37 | + a. If intervals[i][1] > intervals[i+1][0]: return false |
| 38 | +3. Return true |
| 39 | +``` |
| 40 | + |
| 41 | +## Key Insights |
| 42 | + |
| 43 | +- **Sorting**: Sort by start time for efficient checking |
| 44 | +- **Local Optimum**: Check adjacent intervals for overlap |
| 45 | +- **Global Optimum**: No overlapping intervals |
| 46 | +- **Space Optimization**: Use only necessary space |
| 47 | + |
| 48 | +## Alternative Approaches |
| 49 | + |
| 50 | +1. **Brute Force**: Check all pairs |
| 51 | +2. **Sweep Line**: Use sweep line algorithm |
| 52 | +3. **Hash Set**: Use hash set for time slots |
| 53 | + |
| 54 | +## Edge Cases |
| 55 | + |
| 56 | +- Empty intervals: Return true |
| 57 | +- Single interval: Return true |
| 58 | +- No overlaps: Return true |
| 59 | +- All overlaps: Return false |
| 60 | + |
| 61 | +## Applications |
| 62 | + |
| 63 | +- Interval algorithms |
| 64 | +- Scheduling problems |
| 65 | +- Algorithm design patterns |
| 66 | +- Interview preparation |
| 67 | +- System design |
| 68 | + |
| 69 | +## Optimization Opportunities |
| 70 | + |
| 71 | +- **Sorting Algorithm**: Most efficient approach |
| 72 | +- **Space Optimization**: O(1) space complexity |
| 73 | +- **Logarithmic Time**: O(n log n) time complexity |
| 74 | +- **No Extra Space**: Use only necessary space |
0 commit comments