Given M busy-time slots of N people, You need to print all the available time slots when all the N people can schedule a meeting for a duration of K minutes.
Event time will be of form HH MM ( where 0 <= HH <= 23 and 0 <= MM <= 59 ), K will be in the form minutesAn event time slot is of form [Start Time, End Time ) . Which means it inclusive at start time but doesn’t include the end time.
Sample Input: Sample Output:
5 120 00 00 09 00
16 00 17 00 17 00 20 45
10 30 15 30
20 45 22 15
10 00 13 25
09 00 11 00
Approach
The time is expressed as hh mm, and we have 1440 minutes in a day. So we can create a integer array of size 1440, with elements initialized to 0. Now read each value of the busy-time and set the corresponding slot in the integer array to 1s. For instance 01:28 is at array position 88. Since its 1 hour and 28 minutes.
Once all the busy-time slots are mapped to the integer array, then you can find the free slot of given duration. So to find a free slot, start from integer array position 0 and search for consecutive 0s of length atleast 'duration'. All such durations are the free durations.
Solution
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
import java.text.DecimalFormat; | |
import java.text.NumberFormat; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Meeting Schedule calculator | |
* | |
* Output | |
* 0000 - 0900 | |
* 1700 - 2045 | |
* @author raju rama krishna | |
* | |
*/ | |
public class MeetingPlanner { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) throws Exception { | |
List<String> slots = CalendarSlot.getBusySlots(); | |
int n = 24 * 60; // 24 hours * 60 min | |
int[] watch = new int[n]; | |
String[] sArr = new String[2]; | |
for( String s: slots ) { | |
sArr = s.split(" "); | |
String start = sArr[0]; | |
int startMin = Integer.parseInt(start.substring(2)) + Integer.parseInt(start.substring(0, 2))*60; | |
String end = sArr[1]; | |
int endMin = Integer.parseInt(end.substring(2)) + Integer.parseInt(end.substring(0, 2))*60; | |
for(int i=startMin; i <= endMin; i++) { | |
watch[i] = 1; | |
} | |
} | |
int duration = 120; | |
int curr = watch[0]; | |
boolean free = ( curr == 0 )? true: false; | |
int i = 0; | |
int j = 0; | |
while( i < n-1 ) { | |
i++; | |
if(watch[i] == curr ) { | |
continue; | |
} else { | |
if( free && (i-j) >= duration ) { | |
System.out.println(getTime(j) + " - " +getTime(i)); | |
} | |
j = i-1; | |
curr = watch[i]; | |
free = !free; | |
} | |
} | |
} | |
private static String getTime( int val ) { | |
int h = val/60; | |
int m = val%60; | |
NumberFormat formatter = new DecimalFormat("00"); | |
return formatter.format(h) + formatter.format(m); | |
} | |
} | |
class CalendarSlot { | |
private static List<String> filledSlot = new ArrayList<String>(); | |
public static List<String> getBusySlots() { | |
filledSlot.add("1600 1700"); | |
filledSlot.add("1030 1530"); | |
filledSlot.add("2045 2215"); | |
filledSlot.add("1000 1325"); | |
filledSlot.add("0900 1100"); | |
return filledSlot; | |
} | |
} |