Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Result {
- /*
- * Complete the 'getEarliestMeetTime' function below.
- *
- * The function is expected to return a STRING.
- * The function accepts following parameters:
- * 1. STRING_ARRAY events
- * 2. INTEGER k
- */
- public static String getEarliestMeetTime(List<String> events, int k) {
- // Difference array for 1440 minutes in a day (00:00 to 23:59)
- int[] diff = new int[1442];
- for (String event : events) {
- String[] parts = event.split(" ");
- // Times are the last two elements in the string
- int start = timeToMin(parts[parts.length - 2]);
- int end = timeToMin(parts[parts.length - 1]);
- diff[start]++;
- diff[end + 1]--;
- }
- int busyCount = 0;
- int currentGap = 0;
- for (int i = 0; i < 1440; i++) {
- busyCount += diff[i];
- if (busyCount == 0) {
- currentGap++;
- if (currentGap == k) {
- int startMin = i - k + 1;
- return String.format("%02d:%02d", startMin / 60, startMin % 60);
- }
- } else {
- currentGap = 0;
- }
- }
- return "-1";
- }
- private static int timeToMin(String time) {
- String[] parts = time.split(":");
- return Integer.parseInt(parts[0]) * 60 + Integer.parseInt(parts[1]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment