package com.fishercoder.solutions;

import java.util.Arrays;
import java.util.PriorityQueue;

public class _253 {
    public static class Solution1 {

        public int minMeetingRooms(int[][] intervals) {
            if (intervals == null || intervals.length == 0) {
                return 0;
            }

            // Sort the intervals by start time
            Arrays.sort(intervals, (a, b) -> a[0] - b[0]);

            // Use a min heap to track the minimum end time of merged intervals
            PriorityQueue<int[]> heap = new PriorityQueue<>(intervals.length, (a, b) -> a[1] - b[1]);

            // start with the first meeting, put it to a meeting room
            heap.offer(intervals[0]);

            for (int i = 1; i < intervals.length; i++) {
                // get the meeting room that finishes earliest
                int[] interval = heap.poll();

                if (intervals[i][0] >= interval[1]) {
                    // if the current meeting starts right after
                    // there's no need for a new room, merge the interval
                    interval[1] = intervals[i][1];
                } else {
                    // otherwise, this meeting needs a new room
                    heap.offer(intervals[i]);
                }

                // don't forget to put the meeting room back
                heap.offer(interval);
            }

            return heap.size();
        }
    }
}


@@ -1429,12 +1429,1319 @@
  %7D%0A    %7D
+%0A%0A    public static class Solution2 %7B%0A        /**%0A         * I'm so glad to have come up with this solution completely on my own on 10/13/2021.%0A         * Drawing on a piece of paper helps A LOT! It helps visualize your thoughts and clear the ambiguity up!%0A         */%0A        public int minMeetingRooms(int%5B%5D%5B%5D intervals) %7B%0A            //I use the meeting's end time as the room indicate and put them into a heap%0A            PriorityQueue%3CInteger%3E rooms = new PriorityQueue%3C%3E();%0A            Arrays.sort(intervals, (a, b) -%3E a%5B0%5D - b%5B0%5D);%0A            for (int i = 0; i %3C intervals.length; i++) %7B%0A                if (rooms.isEmpty()) %7B%0A                    rooms.add(intervals%5Bi%5D%5B1%5D);%0A                %7D else %7B%0A                    if (rooms.peek() %3E intervals%5Bi%5D%5B0%5D) %7B%0A                        //if the room that becomes available the earliest still cannot accommodate this new meeting, then we'll have to add a new room%0A                        rooms.add(intervals%5Bi%5D%5B1%5D);%0A                    %7D else %7B%0A                        //otherwise, we'll just update the room that finishes the earliest with the new finish time.%0A                        rooms.poll();%0A                        rooms.add(intervals%5Bi%5D%5B1%5D);%0A                    %7D%0A                %7D%0A            %7D%0A            return rooms.size();%0A        %7D%0A    %7D
 %0A%7D%0A%0A

refactor 253