Created
December 21, 2017 04:12
-
-
Save aire-con-gas/a7af82df091dea84f31ed72124d5ce92 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| Given inputs that represent overlapping times such as | |
| [[6, 10], [7, 11], [8, 10], [11, 14], [12, 14], [15, 16]] | |
| where each pair represents [startTime, endTime] | |
| return an array that groups each time pair into an array of overlapping | |
| groups as such: | |
| [ | |
| [ | |
| [6, 10], | |
| [7, 11], | |
| [8, 10] | |
| ], | |
| [ | |
| [11, 14], | |
| [12, 14] | |
| ], | |
| [ | |
| [15, 16] | |
| ] | |
| ] | |
| */ | |
| const overlappingTimeGroups = (inputTimes = []) => { | |
| if (inputTimes.length <= 1) { | |
| return inputTimes; | |
| } | |
| let newTimeGroups = []; | |
| let newTimeGroup = []; | |
| let lowerBoundTime = inputTimes[0][0]; | |
| let upperBoundTime = inputTimes[0][1]; | |
| for(let i = 0; i < inputTimes.length; i++) { | |
| const timePair = inputTimes[i]; | |
| const startTime = timePair[0]; | |
| const endTime = timePair[1]; | |
| if (startTime < upperBoundTime) { | |
| newTimeGroup.push(timePair); | |
| } else { | |
| newTimeGroups.push(newTimeGroup); | |
| newTimeGroup = [timePair]; | |
| lowerBoundTime = startTime; | |
| upperBoundTime = endTime; | |
| } | |
| if (i === inputTimes.length -1) { | |
| newTimeGroups.push(newTimeGroup); | |
| } | |
| } | |
| return newTimeGroups; | |
| } | |
| const inputTimes = [[6, 10], [7, 11], [8, 10], [11, 14], [12, 14], [15, 16]]; | |
| console.log(overlappingTimeGroups(inputTimes)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment