Untitled

 avatar
unknown
plain_text
20 days ago
1.2 kB
2
Indexable
class Solution {
    public int minGroups(int[][] intervals) {
         int n = intervals.length;
        
        // Create arrays to store start and end points
        int[] start = new int[n];
        int[] end = new int[n];
        
        // Fill the start and end arrays
        for (int i = 0; i < n; i++) {
            start[i] = intervals[i][0];
            end[i] = intervals[i][1];
        }
        
        // Sort the start and end arrays
        Arrays.sort(start);
        Arrays.sort(end);
        
        // Initialize the two pointers and counters
        int count = 0; // Current number of platforms needed
        int ans = 0;   // Maximum platforms needed
        int i = 0, j = 0;

        // Use two pointers to traverse both arrays
        while ((i<n) && (j<n)) {
            // If an interval is starting, increment the count
            if (start[i] <= end[j]) {
                count++;
                ans = Math.max(ans, count);
                i++;
            }
            // If an interval is ending, decrement the count
            else {
                count--;
                j++;
            }
        }
        
        return ans;
    }
}
Leave a Comment