|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -import com.fishercoder.common.classes.Interval; |
4 |
| - |
5 | 3 | import java.util.Arrays;
|
6 | 4 | import java.util.PriorityQueue;
|
7 | 5 |
|
8 | 6 | public class _253 {
|
9 | 7 | public static class Solution1 {
|
10 | 8 |
|
11 |
| - public int minMeetingRooms(Interval[] intervals) { |
| 9 | + public int minMeetingRooms(int[][] intervals) { |
12 | 10 | if (intervals == null || intervals.length == 0) {
|
13 | 11 | return 0;
|
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 = new PriorityQueue<>(intervals.length, (a, b) -> a.end - b.end); |
| 18 | + PriorityQueue<int[]> heap = new PriorityQueue<>(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 (int i = 1; i < intervals.length; i++) {
|
26 | 24 | // get the meeting room that finishes earliest
|
27 |
| - Interval interval = 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]);
|
|
0 commit comments