|
1 | 1 | packagecom.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -importcom.fishercoder.common.classes.Interval; |
4 |
| - |
5 | 3 | importjava.util.Arrays;
|
6 | 4 | importjava.util.PriorityQueue;
|
7 | 5 |
|
8 | 6 | publicclass_253 {
|
9 | 7 | publicstaticclassSolution1 {
|
10 | 8 |
|
11 |
| -publicintminMeetingRooms(Interval[]intervals) { |
| 9 | +publicintminMeetingRooms(int[][]intervals) { |
12 | 10 | if (intervals ==null ||intervals.length ==0) {
|
13 | 11 | return0;
|
14 | 12 | }
|
15 | 13 |
|
16 | 14 | // Sort the intervals by start time
|
17 |
| -Arrays.sort(intervals, (a,b) ->a.start -b.start); |
| 15 | +Arrays.sort(intervals, (a,b) ->a[0] -b[0]); |
18 | 16 |
|
19 | 17 | // Use a min heap to track the minimum end time of merged intervals
|
20 |
| -PriorityQueue<Interval>heap =newPriorityQueue<>(intervals.length, (a,b) ->a.end -b.end); |
| 18 | +PriorityQueue<int[]>heap =newPriorityQueue<>(intervals.length, (a,b) ->a[1] -b[1]); |
21 | 19 |
|
22 | 20 | // start with the first meeting, put it to a meeting room
|
23 | 21 | heap.offer(intervals[0]);
|
24 | 22 |
|
25 | 23 | for (inti =1;i <intervals.length;i++) {
|
26 | 24 | // get the meeting room that finishes earliest
|
27 |
| -Intervalinterval =heap.poll(); |
| 25 | +int[]interval =heap.poll(); |
28 | 26 |
|
29 |
| -if (intervals[i].start >=interval.end) { |
| 27 | +if (intervals[i][0] >=interval[1]) { |
30 | 28 | // if the current meeting starts right after
|
31 | 29 | // there's no need for a new room, merge the interval
|
32 |
| -interval.end =intervals[i].end; |
| 30 | +interval[1] =intervals[i][1]; |
33 | 31 | }else {
|
34 | 32 | // otherwise, this meeting needs a new room
|
35 | 33 | heap.offer(intervals[i]);
|
|