Leetcode | 759. Employee free time difficult area coverage skill problem

Source: Internet
Author: User
Tags ranges

Weare given a list schedule of employees, which represents the working time foreach employee.

Eachemployee has a list of non-overlapping intervals, and these intervals is in Sortedorder.

Returnthe list of finite intervals representing common, positive-length freetime for all employees, also in sorte D order.

Example1:

Input: schedule =[[[1,2],[5,6]],[[1,3]],[[4,10]]

Output: [[3,4]]

Explanation:

There is a total of three employees, and all common

Free time intervals would is [-inf, 1], [3, 4], [Ten, INF].

We discard any intervals that contain INF as they aren ' tfinite.

Example2:

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

Output: [[5,6],[7,9]]

(Eventhough we is representing intervals in the form [x, Y], the objects inside is intervals, not lists or arrays. Forexample, 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] with our answer, as they has zerolength.

Note:

1. Schedule and Schedule[i] is lists with lengths InRange [1, 50].

2.0 <=schedule[i].start < Schedule[i].end <= 10^8.

Give you a bunch of ranges that cover the range to indicate that the covered area has been used, allowing you to output all the limited length of the range that is not covered,

In the solution of the topic of this area is usually a trick, is to use an array n,n[i] record position I start of the range minus the I end of the range, so that iterate through the array n can be any one position how many range coverage, when the scope of the number of 0, This range is the scope of the request, it is also important to note that the last span of 0 of the range and the first coverage of 0 of the range is not involved in the count, because these two ranges are infinite

/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval (): Start (0), end (0) {}
 *     Interval (int s, int e): Start (s), End (e) {}
 *};
 *
/class Solution {public
:
vector<interval> employeefreetime (vector<vector<interval >>& avails) {

	vector<interval> result;
	Map<int, int> Mark;
	for (int i = 0; i < avails.size (), i++)
	{for
		(int j = 0; J < avails[i].size (); + j)
		{
			Mark[avai Ls[i][j].start] + = 1;
			Mark[avails[i][j].end]-= 1;
		}
	}
	int coti = 0;
	Map<int, int>::iterator it;
	Map<int, Int>::iterator NXT;
	it = Mark.begin ();

	while (It! = Mark.end ())
	{
		Coti + = it->second;
		NXT = it;
		if (++it)! = Mark.end () &&coti = = 0)
		{
			result.push_back (Interval (Nxt->first, It->first));
		}
	}
	return result;
}
;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.