扫描线 leetcode 759

Posted bella2017

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了扫描线 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;
    
;

 

以上是关于扫描线 leetcode 759的主要内容,如果未能解决你的问题,请参考以下文章

扫描线及其应用

LeetCode 391. 完美矩形(扫描线) / 318. 最大单词长度乘积 / 563. 二叉树的坡度

LeetCode 218. 天际线问题(扫描线)/ 1818. 绝对差值和/ 1846. 减小和重新排列数组后的最大元素

LeetCode 759. Employee Free Time

[leetcode] 75. 分类颜色(常数空间且只扫描一次算法)

leetcode-14.最长公共前缀(图)