Created
May 30, 2017 00:02
-
-
Save jpoechill/15fc4108bad3f7f17a331ebb65e2ce71 to your computer and use it in GitHub Desktop.
Merged Meetings via. Interview Cake
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
// Final solution | |
function mergeRanges (meetings) { | |
// sort by start time | |
var sortedMeetings = meetings.slice().sort(function (a, b) { | |
return a.startTime > b.startTime ? 1: -1 | |
}) | |
// initialize meetings with first from sorted meetings | |
var mergedMeetings = [sortedMeetings[0]] | |
for (var i = 0; i < sortedMeetings.length; i++) { | |
var currentMeeting = sortedMeetings[i]; | |
var lastMergedMeeting = mergedMeetings[mergedMeetings.length - 1] | |
if (currentMeeting.startTime <= lastMergedMeeting.endTime) { | |
lastMergedMeeting = Math.max(lastMergedMeeting.endTime, currentMeeting.endTime) | |
} else { | |
mergedMeetings.push(currentMeeting) | |
} | |
} | |
return mergedMeetings; | |
// console.log(sortedMeetings); | |
} | |
var myMeetings = [ | |
{startTime: 1, endTime: 3}, | |
{startTime: 2, endTime: 4}, | |
{startTime: 0, endTime: 1} | |
] | |
console.log(mergeRanges(myMeetings)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment