Created
April 2, 2018 07:13
-
-
Save jianminchen/a28a4aa28b6ac09dd3546bc60678ffdc to your computer and use it in GitHub Desktop.
meeting room interval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Iterate through all the interval of input, let's call it itv, | |
// For each itv, find a Interval in the Set which ends is less than or equal to itv.start | |
// -> This one, we can use binary search, because the Set is sorted based on it's end | |
// If no such Interval exists in the Set, create a new Interval, and append it to the Set | |
// If there is such Interval, update it's start and end to itv.start and itv.end | |
Example: | |
_________________________________________________________ | |
__________ ______________ | |
______ | |
_______ | |
Solution 2: provided by Julia | |
sweep line algorithm | |
|______________________ 0, 30 | |
| | |
| _____ ______ | |
| | |
5 10 15 20 | |
sort by start time, 0, 5, 15, O(nlogn) | |
sort by end time, 10, 20, 30, O(nlogn) | |
two pointert -> lef right | |
0 - +1 | |
5 - +1 -> maximum = 2 | |
10 - close -1 -> go back to 1 | |
15 - +1 , maximum = 2 | |
20 - - 1, maximum = 1 | |
30 - | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment