Untitled

 avatar
unknown
plain_text
23 days ago
1.5 kB
0
Indexable
import java.util.*;

class Solution {
    public List<Integer> minAvailableDuration(int[][] slots1, int[][] slots2, int duration) {
        // Sort both slots arrays by their start times
        Arrays.sort(slots1, (a, b) -> Integer.compare(a[0], b[0]));
        Arrays.sort(slots2, (a, b) -> Integer.compare(a[0], b[0]));

        int i = 0, j = 0;

        // Use two pointers to find the overlapping intervals
        while (i < slots1.length && j < slots2.length) {
            // Find the overlap between slots1[i] and slots2[j]
            int start = Math.max(slots1[i][0], slots2[j][0]);
            int end = Math.min(slots1[i][1], slots2[j][1]);

            // Check if the overlap is sufficient for the meeting duration
            if (end - start >= duration) {
                return Arrays.asList(start, start + duration);
            }

            // Move the pointer for the interval that ends earlier
            if (slots1[i][1] < slots2[j][1]) {
                i++;
            } else {
                j++;
            }
        }

        // No valid time slot found
        return new ArrayList<>();
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[][] slots1 = {{10, 50}, {60, 120}, {140, 210}};
        int[][] slots2 = {{0, 15}, {60, 70}};
        int duration = 8;

        System.out.println(solution.minAvailableDuration(slots1, slots2, duration)); // Output: [60, 68]
    }
}
Leave a Comment