(similar to 253 )2406. Divide Intervals Into Minimum Number of Groups

actually it's same meaning to 253, but inclusive end time

linesweep

end time is inclusive, so need to add 1

class Solution {
    public int minGroups(int[][] intervals) {
        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int[] interval : intervals) {
            map.put(interval[0], map.getOrDefault(interval[0], 0) + 1);
            map.put(interval[1]+1, map.getOrDefault(interval[1]+1, 0) - 1);
        }

        int roomCount = 0;
        int result = 0;
        for (int time : map.keySet()) {
            roomCount += map.get(time);
            result = Math.max(result, roomCount);
        }
        return result;
    }
}

Heap (1942 style)

result + 1, because it's the group no, we need count, so +1

Heap

one heap

Last updated

Was this helpful?