Advertisement
aqibm

Untitled

Mar 22nd, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. struct Interval {
  7. int start;
  8. int end;
  9. };
  10.  
  11. // Comparison function to sort intervals by start time.
  12. bool compareIntervals(const Interval &a, const Interval &b) {
  13. return a.start < b.start;
  14. }
  15.  
  16. // Two-pointer filtering: Remove meeting intervals that overlap with any not-available interval.
  17. vector<Interval> filterMeetings(const vector<Interval>& meetings, const vector<Interval>& notAvail) {
  18. vector<Interval> result;
  19. int m = meetings.size(), n = notAvail.size();
  20. int i = 0, j = 0;
  21.  
  22. while (i < m) {
  23. // Advance j until we find a not-available interval that might overlap.
  24. while (j < n && notAvail[j].end < meetings[i].start) {
  25. j++;
  26. }
  27.  
  28. bool overlaps = false;
  29. // Check if the current meeting overlaps with the current not-available interval.
  30. if (j < n && notAvail[j].start <= meetings[i].end) {
  31. overlaps = true;
  32. }
  33.  
  34. if (!overlaps) {
  35. result.push_back(meetings[i]);
  36. }
  37. i++;
  38. }
  39. return result;
  40. }
  41.  
  42. // Merge overlapping meeting intervals.
  43. vector<Interval> mergeIntervals(const vector<Interval>& intervals) {
  44. if (intervals.empty()) return {};
  45. vector<Interval> merged;
  46. Interval current = intervals[0];
  47. for (size_t i = 1; i < intervals.size(); i++) {
  48. // If the next interval overlaps or touches the current one, merge them.
  49. if (intervals[i].start <= current.end) {
  50. current.end = max(current.end, intervals[i].end);
  51. } else {
  52. merged.push_back(current);
  53. current = intervals[i];
  54. }
  55. }
  56. merged.push_back(current);
  57. return merged;
  58. }
  59.  
  60. int main(){
  61. // Example Input:
  62. // Meeting intervals (each as [start, end])
  63. vector<Interval> meetings = {
  64. {9, 10},
  65. {9, 11},
  66. {13, 14}
  67. };
  68.  
  69. // Not-available intervals
  70. vector<Interval> notAvailable = {
  71. {10, 10} // For example, the person is not available at time 10.
  72. };
  73.  
  74. // Sort both lists by start time
  75. sort(meetings.begin(), meetings.end(), compareIntervals);
  76. sort(notAvailable.begin(), notAvailable.end(), compareIntervals);
  77.  
  78. // Filter meetings: remove any meeting that overlaps with a not-available interval.
  79. vector<Interval> filtered = filterMeetings(meetings, notAvailable);
  80.  
  81. // Now merge the remaining meeting intervals (if any overlap).
  82. vector<Interval> merged = mergeIntervals(filtered);
  83.  
  84. // Output the final intervals.
  85. cout << "Meetings the person can attend (merged):" << endl;
  86. for (const auto &in : merged) {
  87. cout << "[" << in.start << ", " << in.end << "]" << endl;
  88. }
  89.  
  90. return 0;
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement