[Neetcode] Meeting Rooms II

whitehousechef·2025년 8월 12일

https://neetcode.io/problems/meeting-schedule-ii?list=neetcode150

initial

as long as u get the pattern its solveable. i think sort via start time and storing each end times in a heap and if current iterating start time is greater or equal to the minimum end time in heap, we pop that in heap and add the new end date? else we just add the current end date to heap cuz we need extra room? That is exactly correct

sol

class Solution {
    public int minMeetingRooms(List<Interval> intervals) {
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        intervals.sort(Comparator.comparingInt(interval->interval.start));
        for(Interval interval:intervals){
            if(pq.isEmpty()){
                pq.offer(interval.end);
                continue;
            } 
            if(interval.start>=pq.peek()){
                pq.poll();
            } 
            pq.offer(interval.end);
        }
        return pq.size();

    }
}

complexity

n log n cuz of sort
worst case n space

0개의 댓글