package com.fishercoder.solutions;

import com.fishercoder.common.classes.Interval;

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

/**
 * Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei),
 * find the minimum number of conference rooms required.

 For example,
 Given [[0, 30],[5, 10],[15, 20]],
 return 2.
 */
public class _253 {

    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();
    }
}

@@ -140,266 +140,69 @@
 e;%0A%0A
-/**%0A * Given an array of meeting time intervals consisting of start and end times %5B%5Bs1,e1%5D,%5Bs2,e2%5D,...%5D (si %3C ei),%0A * find the minimum number of conference rooms required.%0A%0A For example,%0A Given %5B%5B0, 30%5D,%5B5, 10%5D,%5B15, 20%5D%5D,%0A return 2.%0A */%0Apublic class _253 %7B%0A%0A
+public class _253 %7B%0A    public static class Solution1 %7B%0A%0A    
     
@@ -252,32 +252,36 @@
 vals) %7B%0A        
+    
 if (intervals ==
@@ -322,24 +322,28 @@
             
+  
+  
 return 0;%0A  
@@ -340,35 +340,43 @@
 turn 0;%0A        
-%7D%0A%0A
+    %7D%0A%0A    
         // Sort 
@@ -411,16 +411,20 @@
         
+ 
+   
 Arrays.s
@@ -477,16 +477,20 @@
         
+   
+ 
 // Use a
@@ -549,16 +549,20 @@
 tervals%0A
+    
         
@@ -665,16 +665,20 @@
         
+ 
+   
 // start
@@ -727,32 +727,36 @@
 ng room%0A        
+    
 heap.offer(inter
@@ -762,24 +762,28 @@
 rvals%5B0%5D);%0A%0A
+    
         for 
@@ -835,16 +835,20 @@
         
+    
 // get t
@@ -898,16 +898,20 @@
         
+    
 Interval
@@ -940,32 +940,36 @@
 );%0A%0A            
+   
+ 
 if (intervals%5Bi%5D
@@ -990,24 +990,28 @@
 rval.end) %7B%0A
+    
             
@@ -1075,16 +1075,20 @@
         
+ 
+   
 // there
@@ -1149,16 +1149,20 @@
         
+  
+  
 interval
@@ -1186,16 +1186,20 @@
 i%5D.end;%0A
+    
         
@@ -1227,16 +1227,20 @@
         
+    
 // other
@@ -1283,32 +1283,36 @@
                 
+ 
+   
 heap.offer(inter
@@ -1325,35 +1325,43 @@
 %5D);%0A            
-%7D%0A%0A
+    %7D%0A%0A    
             // d
@@ -1405,32 +1405,36 @@
 ack%0A            
+    
 heap.offer(inter
@@ -1447,27 +1447,34 @@
         
-%7D%0A%0A
+    %7D%0A%0A   
         
 return h
@@ -1461,24 +1461,25 @@
 %0A           
+ 
 return heap.
@@ -1490,12 +1490,22 @@
 ();%0A
+        %7D%0A
     %7D%0A%7D%0A
@@ -1504,8 +1504,9 @@
     %7D%0A%7D%0A
+%0A

refactor 253