package com.fishercoder.solutions;

import com.fishercoder.common.classes.Interval;

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

public class _253 {
    public static class Solution1 {

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

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

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

            // 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
                Interval interval = heap.poll();

                if (intervals[i].start >= interval.end) {
                    // if the current meeting starts right after
                    // there's no need for a new room, merge the interval
                    interval.end = intervals[i].end;
                } 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();
        }
    }
}


@@ -33,57 +33,8 @@
 s;%0A%0A
-import com.fishercoder.common.classes.Interval;%0A%0A
 impo
@@ -179,24 +179,21 @@
 ngRooms(
-Interval
+int%5B%5D
 %5B%5D inter
@@ -401,24 +401,18 @@
 -%3E a
-.start - b.start
+%5B0%5D - b%5B0%5D
 );%0A%0A
@@ -517,24 +517,21 @@
 tyQueue%3C
-Interval
+int%5B%5D
 %3E heap =
@@ -584,20 +584,18 @@
 -%3E a
-.end - b.end
+%5B1%5D - b%5B1%5D
 );%0A%0A
@@ -843,16 +843,13 @@
     
-Interval
+int%5B%5D
  int
@@ -906,14 +906,11 @@
 s%5Bi%5D
-.start
+%5B0%5D
  %3E= 
@@ -917,20 +917,19 @@
 interval
-.end
+%5B1%5D
 ) %7B%0A    
@@ -1091,20 +1091,19 @@
 interval
-.end
+%5B1%5D
  = inter
@@ -1113,12 +1113,11 @@
 s%5Bi%5D
-.end
+%5B1%5D
 ;%0A  

refactor 253