LeetCode 759. 员工空闲时间(排序)
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 759. 员工空闲时间(排序)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 題目
- 2. 解題
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<Interval> employeeFreeTime(vector<vector<Interval>> schedule) {int l = INT_MAX;vector<Interval> 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<Interval> 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博客地址 https://michael.blog.csdn.net/
長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!
總結
以上是生活随笔為你收集整理的LeetCode 759. 员工空闲时间(排序)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 351. 安卓系统手势
- 下一篇: LeetCode 546. 移除盒子(D