LeetCode1月7号比赛题目 Employee Free Time

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.


题目大意:给出n个员工的工作时间,求出这n个员工的共同休息时间。

思路很直接暴力。先用一个List<List<Intervals>>存放各个员工的休息时间,然后用一个中间变量(代码里面命名为temp)List<Intervals>存储第一个员工的休息时间,然后和剩下的所有员工进行比较,取出其公共休息时间放到另一个(代码里面命名为ans)List<Intervals>里面
需要注意,每次和一个员工比较完毕之后,用ans的内容覆盖temp的内容,最后一次除外。最后返回ans即可。
代码如下

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
class Solution {
    public List<Interval> employeeFreeTime(List<List<Interval>> avails) {
		int min = Integer.MAX_VALUE;
		List<Interval> temp = avails.get(avails.size() - 1);
		int max=Integer.MIN_VALUE;
        //这里先求出最小值和最大值,但是题目说了有排序,所以可能没什么卵用。
		for(int i=0;i<avails.size();i++) {
			int size=avails.get(i).size();
			max=Math.max(max, avails.get(i).get(size-1).end);
			min=Math.min(min,avails.get(i).get(0).start);
		}
        //逐个求出每个员工的休息时间,空闲时间是这个时间的结尾和下个时间的开头,开头和结尾需要单独拿出来考虑
		List<List<Interval>> freetime = new ArrayList<>();
		for (int i = 0; i < avails.size(); i++) {
			temp = avails.get(i);
			List<Interval> ft = new ArrayList<>();
			int start=avails.get(i).get(0).start;
			if(start!=min) {
				Interval s = new Interval(min, start);
				ft.add(s);
			}
			for (int j = 0; j < temp.size() - 1; j++) {
				int firststart = temp.get(j).start;
				int firstend = temp.get(j).end;
				int secondstart = temp.get(j + 1).start;
				int secondend = temp.get(j + 1).end;
				if (firstend != secondstart) {
					Interval s = new Interval(firstend, secondstart);
					ft.add(s);
				}
			}
			int end=temp.get(temp.size()-1).end;
			if(end!=max) {
				Interval s = new Interval(end, max);
				ft.add(s);
			}
			freetime.add(ft);
		}
        //temp初始化为第一个员工的休息时间
		temp = new ArrayList<>(freetime.get(0));
		if(avails.size()==1)
			return temp;
		List<Interval> ans = new ArrayList<>();
		for (int i = 1; i < avails.size(); i++) {
			List<Interval> temp2 = freetime.get(i);
			for (int j = 0; j < temp.size(); j++) {
				int tempstart = temp.get(j).start;
				int tempend = temp.get(j).end;
				for (int k = 0; k < temp2.size(); k++) {
					int temp2start = temp2.get(k).start;
					int temp2end = temp2.get(k).end;
                    //前面两种情况分别是temp取出来的时间段和当前该员工的时间段无交集
					if (tempend <= temp2start) {
						break;
					} else if (tempstart >= temp2end) {
						continue;
					} else {
						Interval interval = new Interval(Math.max(temp2start, tempstart), Math.min(tempend, temp2end));
						ans.add(interval);
					}
				}
			}
			temp.clear();
			temp.addAll(ans);
			if (i != avails.size() - 1) {
				ans.clear();
			}
		}
		return ans;
	}
}


第一次参加这个比赛,代码写的有点凌乱,但是思路还是很清晰的。。。。。

猜你喜欢

转载自blog.csdn.net/KingYMxe/article/details/78994189
今日推荐