|
5 | 5 |
|
6 | 6 | public class _253 {
|
7 | 7 | public static class Solution1 {
|
8 |
| - |
9 | 8 | public int minMeetingRooms(int[][] intervals) {
|
10 | 9 | if (intervals == null || intervals.length == 0) {
|
11 | 10 | return 0;
|
12 | 11 | }
|
13 |
| - |
14 |
| - // Sort the intervals by start time |
15 |
| - Arrays.sort(intervals, (a, b) -> a[0] - b[0]); |
16 |
| - |
17 |
| - // Use a min heap to track the minimum end time of merged intervals |
18 |
| - PriorityQueue<int[]> heap = new PriorityQueue<>(intervals.length, (a, b) -> a[1] - b[1]); |
19 |
| - |
20 |
| - // start with the first meeting, put it to a meeting room |
21 |
| - heap.offer(intervals[0]); |
22 |
| - |
| 12 | + Arrays.sort(intervals, (a, b) -> a[0] - b[0]);// Sort the intervals by start time |
| 13 | + PriorityQueue<int[]> heap = new PriorityQueue<>(intervals.length, (a, b) -> a[1] - b[1]);// Use a min heap to track the minimum end time of merged intervals |
| 14 | + heap.offer(intervals[0]);// start with the first meeting, put it to a meeting room |
23 | 15 | for (int i = 1; i < intervals.length; i++) {
|
24 | 16 | // get the meeting room that finishes earliest
|
25 |
| - int[] interval = heap.poll(); |
26 |
| - |
27 |
| - if (intervals[i][0] >= interval[1]) { |
| 17 | + int[] last = heap.poll(); |
| 18 | + if (intervals[i][0] >= last[1]) { |
28 | 19 | // if the current meeting starts right after
|
29 | 20 | // there's no need for a new room, merge the interval
|
30 |
| - interval[1] = intervals[i][1]; |
| 21 | + last[1] = intervals[i][1]; |
31 | 22 | } else {
|
32 | 23 | // otherwise, this meeting needs a new room
|
33 | 24 | heap.offer(intervals[i]);
|
34 | 25 | }
|
35 |
| - |
36 | 26 | // don't forget to put the meeting room back
|
37 |
| - heap.offer(interval); |
| 27 | + heap.offer(last); |
38 | 28 | }
|
39 | 29 |
|
40 | 30 | return heap.size();
|
41 | 31 | }
|
42 | 32 | }
|
43 |
| - |
44 |
| - public static class Solution2 { |
45 |
| - /** |
46 |
| - * I'm so glad to have come up with this solution completely on my own on 10/13/2021. |
47 |
| - * Drawing on a piece of paper helps A LOT! It helps visualize your thoughts and clear the ambiguity up! |
48 |
| - */ |
49 |
| - public int minMeetingRooms(int[][] intervals) { |
50 |
| - //I use the meeting's end time as the room indicate and put them into a heap |
51 |
| - PriorityQueue<Integer> rooms = new PriorityQueue<>(); |
52 |
| - Arrays.sort(intervals, (a, b) -> a[0] - b[0]); |
53 |
| - for (int i = 0; i < intervals.length; i++) { |
54 |
| - if (rooms.isEmpty()) { |
55 |
| - rooms.add(intervals[i][1]); |
56 |
| - } else { |
57 |
| - if (rooms.peek() > intervals[i][0]) { |
58 |
| - //if the room that becomes available the earliest still cannot accommodate this new meeting, then we'll have to add a new room |
59 |
| - rooms.add(intervals[i][1]); |
60 |
| - } else { |
61 |
| - //otherwise, we'll just update the room that finishes the earliest with the new finish time. |
62 |
| - rooms.poll(); |
63 |
| - rooms.add(intervals[i][1]); |
64 |
| - } |
65 |
| - } |
66 |
| - } |
67 |
| - return rooms.size(); |
68 |
| - } |
69 |
| - } |
70 | 33 | }
|
71 | 34 |
|
0 commit comments