fork download
  1. You're asking for the **Leetcode "Meeting Rooms"** problem — there are **two variants**:
  2.  
  3. ---
  4.  
  5. ### ✅ **1. Leetcode 252 – Meeting Rooms**
  6.  
  7. **Problem**: Given an array of meeting time intervals `intervals` where `intervals[i] = [start, end]`, determine if a person could attend all meetings.
  8.  
  9. ---
  10.  
  11. ### 🔹 **C++ Solution:**
  12.  
  13. ```cpp
  14. class Solution {
  15. public:
  16. bool canAttendMeetings(vector<vector<int>>& intervals) {
  17. sort(intervals.begin(), intervals.end());
  18.  
  19. for (int i = 1; i < intervals.size(); ++i) {
  20. if (intervals[i][0] < intervals[i - 1][1]) {
  21. return false; // overlap
  22. }
  23. }
  24. return true;
  25. }
  26. };
  27. ```
  28.  
  29. ---
  30.  
  31. ### ✅ **2. Leetcode 253 – Meeting Rooms II**
  32.  
  33. **Problem**: Return the **minimum number of conference rooms required**.
  34.  
  35. ---
  36.  
  37. ### 🔹 **Optimized C++ Solution (Using Min Heap):**
  38.  
  39. ```cpp
  40. class Solution {
  41. public:
  42. int minMeetingRooms(vector<vector<int>>& intervals) {
  43. if (intervals.empty()) return 0;
  44.  
  45. // Sort meetings by start time
  46. sort(intervals.begin(), intervals.end());
  47.  
  48. // Min-heap to keep track of end times
  49. priority_queue<int, vector<int>, greater<int>> minHeap;
  50.  
  51. for (auto& meeting : intervals) {
  52. // If the earliest meeting ends before the current one starts, reuse room
  53. if (!minHeap.empty() && minHeap.top() <= meeting[0]) {
  54. minHeap.pop();
  55. }
  56. minHeap.push(meeting[1]); // add current meeting's end time
  57. }
  58.  
  59. return minHeap.size(); // Number of rooms used
  60. }
  61. };
  62. ```
  63.  
  64. ---
  65.  
  66. ### 🧠 Explanation:
  67.  
  68. * **Sort by start time.**
  69. * Use a **min-heap** to track current meetings’ end times.
  70. * If a meeting can reuse a room (start time ≥ earliest end time), pop the heap.
  71. * Push the current meeting's end time to the heap.
  72. * The **heap size = number of rooms needed**.
  73.  
  74. ---
  75.  
  76. Let me know if you want the **Python version**, **visual explanation**, or **handling edge cases** like duplicate meetings.
  77.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty