扫描线 leetcode 759

 

/*
// 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) {
        vector<Interval*> all;
        for(auto i : schedule)
            all.insert(all.end(), i.begin(), i.end());   //将每个员工的工作区间加入到all中
        
        //将all里数据从小到大排序
        sort(all.begin(), all.end(), [](const Interval* a, const Interval* b){
            return a->start < b->start;
        });
        
        vector<Interval*> ans;
        int end = all.front()->end;
        for(auto busy : all){
            if(busy->start > end){
                busy->start;
                Interval* a = new Interval(end, busy->start); 
                ans.push_back(a);
            }
            end = max(end, busy->end);
        }
        return ans;
    }
};

 

参考链接:https://zxi.mytechroad.com/blog/geometry/leetcode-759-employee-free-time/

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Author: Huahua
// Running time: 81 ms
class Solution {
public:
    vector<Interval> employeeFreeTime(vector<vector<Interval>>& schedule) {
      vector<Interval> all;
      for (const auto intervals : schedule)
        all.insert(all.end(), intervals.begin(), intervals.end());
      std::sort(all.begin(), all.end(), 
                [](const Interval& a, const Interval& b){
                  return a.start < b.start;
                });
      vector<Interval> ans;
      int end = all.front().end;
      for (const Interval& busy : all) {
        if (busy.start > end) 
          ans.emplace_back(end, busy.start);  
        end = max(end, busy.end);
      }
      return ans;
    }
};

 

posted @ 2019-08-27 23:23  爱学英语的程序媛  阅读(292)  评论(0编辑  收藏  举报