Iam_Sandeep

Untitled

Feb 3rd, 2026
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Result {
  4.  
  5.     /*
  6.      * Complete the 'getEarliestMeetTime' function below.
  7.      *
  8.      * The function is expected to return a STRING.
  9.      * The function accepts following parameters:
  10.      * 1. STRING_ARRAY events
  11.      * 2. INTEGER k
  12.      */
  13.  
  14.     public static String getEarliestMeetTime(List<String> events, int k) {
  15.         // Difference array for 1440 minutes in a day (00:00 to 23:59)
  16.         int[] diff = new int[1442];
  17.  
  18.         for (String event : events) {
  19.             String[] parts = event.split(" ");
  20.             // Times are the last two elements in the string
  21.             int start = timeToMin(parts[parts.length - 2]);
  22.             int end = timeToMin(parts[parts.length - 1]);
  23.            
  24.             diff[start]++;
  25.             diff[end + 1]--;
  26.         }
  27.  
  28.         int busyCount = 0;
  29.         int currentGap = 0;
  30.  
  31.         for (int i = 0; i < 1440; i++) {
  32.             busyCount += diff[i];
  33.            
  34.             if (busyCount == 0) {
  35.                 currentGap++;
  36.                 if (currentGap == k) {
  37.                     int startMin = i - k + 1;
  38.                     return String.format("%02d:%02d", startMin / 60, startMin % 60);
  39.                 }
  40.             } else {
  41.                 currentGap = 0;
  42.             }
  43.         }
  44.  
  45.         return "-1";
  46.     }
  47.  
  48.     private static int timeToMin(String time) {
  49.         String[] parts = time.split(":");
  50.         return Integer.parseInt(parts[0]) * 60 + Integer.parseInt(parts[1]);
  51.     }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment