leetcode:759. 员工空闲时间

题目来源

题目描述

给定员工的 schedule 列表,表示每个员工的工作时间

每个员工都有一个非重叠的时间段 Intervals 列表,这些时间段已经排好序

返回表示 所有 员工的 共同,正数长度的空闲时间 的有限时间段的列表,同样需要排好序。

示例 1:
输入:schedule = [[[1,2],[5,6]],[[1,3]],[[4,10]]]
输出:[[3,4]]
解释:
共有 3 个员工,并且所有共同的
空间时间段是 [-inf, 1], [3, 4], [10, inf]。
我们去除所有包含 inf 的时间段,因为它们不是有限的时间段。
 
示例 2:
输入:schedule = [[[1,3],[6,7]],[[2,4]],[[2,5],[9,12]]]
输出:[[5,6],[7,9]]
 

而且,答案中不包含 [5, 5] ,因为长度为 0。
schedule 和 schedule[i] 为长度范围在 [1, 50]的列表。
0 <= schedule[i].start < schedule[i].end <= 10^8
class Interval {
public:
    int start;
    int end;

    Interval() {}

    Interval(int _start, int _end) {
        start = _start;
        end = _end;
    }
};
class Solution {
public:
    vector<Interval> employeeFreeTime(vector<vector<Interval>> schedule)  {

    }
};


题目解析

例子一

在这里插入图片描述

分析

  • 本质:求合并后中间不相连的区间

  • 怎么合并呢?

    • 因为休息时间和员工个人无关,所以将二维数组转换为一维数组
    • 然后按照起始区间从小大大排序,然后合并
class Solution {
public:
    vector<Interval> employeeFreeTime(vector<vector<Interval>> schedule)  {
        vector<Interval> tmp;
        for(auto a : schedule){
            tmp.insert(tmp.end(), a.begin(), a.end());
        }

        //按照起始区间排序
        std::sort(tmp.begin(), tmp.end(), [](Interval &a, Interval &b){
            return a.start < b.start;
        });

        // 开始进行区间合并
        vector<Interval> merge;
        merge.push_back(tmp[0]);
        for (int i = 1; i < tmp.size(); ++i) {
            if(tmp[i].start < merge.back().end){ // 还没有结束
                merge.back().end = tmp[i].end;
            }else{
                merge.push_back(tmp[i]); //一个新区间
            }
        }

        // [1, 3] ---[4, 10]
        // 开始找不相连的区间
        vector<Interval> ans;
        // [0, 1]   [2, 3]  [6, 7]
        //    2 往前看
        //    6 往前看
        for (int i = 1; i < merge.size(); ++i) {
            ans.emplace_back(merge[i - 1].end, merge[i].start);
        }

        return ans;
    }
};


实现二:

class Solution {
public:
    vector<Interval> employeeFreeTime(vector<vector<Interval>>& schedule) {
        vector<Interval> res, v;
        for (auto a : schedule) {
            v.insert(v.end(), a.begin(), a.end());
        }
        sort(v.begin(), v.end(), [](Interval &a, Interval &b) {return a.start < b.start;});
        Interval t = v[0];
        for (Interval i : v) {
            if (t.end < i.start) {
                res.push_back(Interval(t.end, i.start));
                t = i;
            } else {
                t = (t.end < i.end) ? i : t;
            }
        }
        return res;
    }
};

测试

int main() {
    vector<vector<Interval>> schedule {
            {{1, 2}, {5, 6}},
            {{1, 3}, {4, 10}}
    };
    Solution a;
    auto ans = a.employeeFreeTime(schedule);
    for(auto t : ans){
        printf("%d, %d\n", t.start, t.end);
    }
    printf("\n");

    schedule  = {
            {{1, 3}, {6, 7}},
            {{2, 4}, {2, 5}, {9, 12}}
    };

    ans = a.employeeFreeTime(schedule);
    for(auto t : ans){
        printf("%d, %d\n", t.start, t.end);
    }
    printf("\n");
}

类似题目

题目
leetcode:56. 合并区间 Merge Intervals先按照区间的起始位置排序,然后判断区间是否重叠,如果不重叠,那么压入一个新的区间,否则(【 起点 在 之前的范围内】) ,那么需要更新右边界
leetcode:759. 员工空闲时间 Employee Free Time合并区间后求不连续的区间
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值