SHARE
    TWEET
    aqibm

    Untitled

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

    AltStyle によって変換されたページ (->オリジナル) /