LeetCode 759. 员工空闲时间(排序)
发布日期:2021-07-01 03:31:30 浏览次数:2 分类:技术文章

本文共 1532 字,大约阅读时间需要 5 分钟。

文章目录

1. 题目

给定员工的 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。

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/employee-free-time

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

/*// Definition for an Interval.class Interval {public:    int start;    int end;    Interval() {}    Interval(int _start, int _end) {        start = _start;        end = _end;    }};*/class Solution {
public: vector
employeeFreeTime(vector
> schedule) {
int l = INT_MAX; vector
v; for(auto& s: schedule) for(auto& i : s) {
v.push_back(i); l = min(l, i.end); } sort(v.begin(), v.end(), [&](auto& a, auto& b){
if(a.start == b.start) return a.end < b.end; return a.start < b.start; }); vector
ans; for(int i = 0; i < v.size(); ++i) {
if(l < v[i].start) ans.push_back(Interval(l, v[i].start)); l = max(l, v[i].end); } return ans; }};

56 ms 10.6 MB


我的CSDN

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!

Michael阿明

转载地址:https://michael.blog.csdn.net/article/details/107967098 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode 656. 金币路径(DP)
下一篇:LeetCode 1088. 易混淆数 II(回溯/BFS)

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年05月07日 14时04分38秒