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

LeetCode 759. Employee Free Time

时间:2018-08-01 12:07:29      阅读:404      评论:0      收藏:0      [点我收藏+]

标签:include   pre   array   efi   ppi   sid   -o   inf   sch   

原题链接在这里:https://leetcode.com/problems/employee-free-time/description/

题目:

We are given a list schedule of employees, which represents the working time for each employee.

Each employee has a list of non-overlapping Intervals, and these intervals are in sorted order.

Return the list of finite intervals representing common, positive-length free time for all employees, also in sorted order.

Example 1:

Input: schedule = [[[1,2],[5,6]],[[1,3]],[[4,10]]]
Output: [[3,4]]
Explanation:
There are a total of three employees, and all common
free time intervals would be [-inf, 1], [3, 4], [10, inf].
We discard any intervals that contain inf as they aren‘t finite.

Example 2:

Input: schedule = [[[1,3],[6,7]],[[2,4]],[[2,5],[9,12]]]
Output: [[5,6],[7,9]]

(Even though we are representing Intervals in the form [x, y], the objects inside are Intervals, not lists or arrays. For example, schedule[0][0].start = 1, schedule[0][0].end = 2, and schedule[0][0][0] is not defined.)

Also, we wouldn‘t include intervals like [5, 5] in our answer, as they have zero length.

Note:

  1. schedule and schedule[i] are lists with lengths in range [1, 50].
  2. 0 <= schedule[i].start < schedule[i].end <= 10^8.

题解:

k条已经排好序的链表. 利用minHeap进行merge. 

先把每个表头放在minHeap中. minHeap按照指向的Interval start排序.

poll出来的就是当前最小start的interval. 如果标记的时间比这个interval的start还小就说明出现了断裂也就是空余时间. 

把标记时间增大到这个interval的end, 并且把这个interval所在链表的后一位加入minHeap中.

Time Complexity: O(nlogk). k是employee的个数, schedule.size(). n是一共有多少interval.

Space: O(k).

AC Java:

 1 /**
 2  * Definition for an interval.
 3  * public class Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() { start = 0; end = 0; }
 7  *     Interval(int s, int e) { start = s; end = e; }
 8  * }
 9  */
10 class Solution {
11     public List<Interval> employeeFreeTime(List<List<Interval>> schedule) {
12         List<Interval> res = new ArrayList<Interval>();
13         PriorityQueue<Node> minHeap = new PriorityQueue<Node>((a,b) -> 
14                                             schedule.get(a.employee).get(a.index).start - schedule.get(b.employee).get(b.index).start);
15         
16         int start = Integer.MAX_VALUE;
17         for(int i = 0; i<schedule.size(); i++){
18             minHeap.add(new Node(i, 0));
19             start = Math.min(start, schedule.get(i).get(0).start);
20         }
21         
22         while(!minHeap.isEmpty()){
23             Node cur = minHeap.poll();
24             if(start < schedule.get(cur.employee).get(cur.index).start){
25                 res.add(new Interval(start, schedule.get(cur.employee).get(cur.index).start));
26             }
27             
28             start = Math.max(start, schedule.get(cur.employee).get(cur.index).end);
29             cur.index++;
30             if(cur.index < schedule.get(cur.employee).size()){
31                 minHeap.add(cur);
32             }
33         }
34         
35         return res;
36     }
37 }
38 
39 class Node{
40     int employee;
41     int index;
42     public Node(int employee, int index){
43         this.employee = employee;
44         this.index = index;
45     }
46 }

 

LeetCode 759. Employee Free Time

标签:include   pre   array   efi   ppi   sid   -o   inf   sch   

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/9399311.html

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