码迷,mamicode.com
首页 > 其他好文 > 详细

扫描线 leetcode 759

时间:2019-08-28 01:06:30      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:als   def   star   col   style   efi   employee   pre   color   

技术图片

技术图片

 

/*
// 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;
    }
};

 

扫描线 leetcode 759

标签:als   def   star   col   style   efi   employee   pre   color   

原文地址:https://www.cnblogs.com/Bella2017/p/11421441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!