[Leetcode] 759. Employee Free Time 员工空闲时间

该文讲述了如何解决一个算法问题,即给定一组员工的工作时间区间,找到所有员工都空闲的时间段。解决方案包括将区间拆分、排序,然后通过遍历找出不重叠的空闲区间。代码示例使用C++实现,通过排序和迭代找到答案。
摘要由CSDN通过智能技术生成

网上看到这篇文章,这里记录巩固一下。

题目

You are given a list schedule of employees,which represents the working time for each 
employee.Each employee has a list of non-overlapping Intervals,and these intervals are
in sorted order.Return the list of finite intervals representing common,positive-length 
free for all employees,alse in sorted order.
 
Example 1:
Input: schedule=[[[1,2],[5,6]],[[1,3]],[[4,10]]]
Output:[[3,4]]
Explanation:
There are a total of three employees, and all common
free time intervals would be [-inf, 1], [3, 4], [10, inf].
We discard any intervals that contain inf as they aren't finite.
Example 2:
Input: schedule=[[[1,3],[6,7]],[[2,4]],[[2,5],[9,12]]]
Output:[[5,6],[7,9]]

题目大意:
    给定一组员工的工作时间,其中每个员工的工作时间是一组排好序的不相交的区间,要求输出所有员工都空闲的区间,不包含首尾的无限区间.


解题

      先拆分一维数组,然后进行排序(以start进行排序,若相等则以end排序,从小到大),最后枚举各个区间看是否重叠.

在这里插入图片描述

#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
struct Interval{
    int start;
    int end;
    Interval():start(0),end(0){}
    Interval(int s,int e):start(s),end(e){}
};
 
// class MyCompare{
// public:
//     bool operator()(const Interval& a,const Interval& b){
//         return a.start==b.start?(a.end<b.end):(a.start<b.start);
//     }
// };
 
class Solution{
public:
    vector<Interval> employeeFreeTime(vector<vector<Interval>>& schedule){
        vector<Interval> board;
        for(auto& sch:schedule){
            for(auto& i:sch){
                board.push_back(i);
            }
        }
        //sort(board.begin(),board.end(),MyCompare());
        sort(board.begin(),board.end(),[](const Interval& a,const Interval& b){
            if(a.start==b.start){
                return a.end<b.end;
            }
            return a.start<b.start;
        });
        if(board.size()==1)  return vector<Interval>{};
 
        vector<Interval> ans;  
        int i=1,L=board[0].end;
        while(i<board.size()){
            if(board[i].start<=L){
                L=max(board[i].end,L);
            }else{
                ans.push_back({L,board[i].start});
                L=board[i].end;
            }
            i++;
        }
 
        // for(auto& an:board){
        //     cout<<"["<<an.start<<","<<an.end<<"]";
        // }
        // cout<<endl;
        // for(auto& an:ans){
        //     cout<<"["<<an.start<<","<<an.end<<"]";
        // }
        return ans;
    }
};
 
int main(int argc,char* argv[]){
    vector<vector<Interval>> schedule={{{1,2},{5,6}},{{1,3}},{{4,10}}};//{{{1,3},{6,7}},{{2,4}},{{2,5},{9,12}}};
    vector<Interval> ans=Solution().employeeFreeTime(schedule);
    for(auto& an:ans){
        cout<<"["<<an.start<<","<<an.end<<"]";
    }
    cout<<endl;
    return 0;
}

参考: 

LeetCode:输出所有员工都空闲的区间_求所有员工的空闲实际_路上的追梦人的博客-CSDN博客

leetcode:759. 员工空闲时间_员工空闲时间 牛客_OceanStar的学习笔记的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值