LeetCode 759. 员工空闲时间(排序)
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 759. 员工空闲时间(排序)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 1. 題目
- 2. 解題
1. 題目
給定員工的 schedule 列表,表示每個(gè)員工的工作時(shí)間。
每個(gè)員工都有一個(gè)非重疊的時(shí)間段 Intervals 列表,這些時(shí)間段已經(jīng)排好序。
返回表示 所有 員工的 共同,正數(shù)長度的空閑時(shí)間 的有限時(shí)間段的列表,同樣需要排好序。
示例 1: 輸入:schedule = [[[1,2],[5,6]],[[1,3]],[[4,10]]] 輸出:[[3,4]] 解釋: 共有 3 個(gè)員工,并且所有共同的 空間時(shí)間段是 [-inf, 1], [3, 4], [10, inf]。 我們?nèi)コ邪?inf 的時(shí)間段,因?yàn)樗鼈儾皇怯邢薜臅r(shí)間段。示例 2: 輸入:schedule = [[[1,3],[6,7]],[[2,4]],[[2,5],[9,12]]] 輸出:[[5,6],[7,9]]而且,答案中不包含 [5, 5] ,因?yàn)殚L度為 0。 schedule 和 schedule[i] 為長度范圍在 [1, 50]的列表。 0 <= schedule[i].start < schedule[i].end <= 10^8。來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/employee-free-time
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。
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/
長按或掃碼關(guān)注我的公眾號(hào)(Michael阿明),一起加油、一起學(xué)習(xí)進(jìn)步!
總結(jié)
以上是生活随笔為你收集整理的LeetCode 759. 员工空闲时间(排序)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 351. 安卓系统手势
- 下一篇: LeetCode 546. 移除盒子(D