@@ -40,5 +40,32 @@ public int minMeetingRooms(int[][] intervals) {
40
40
return heap .size ();
41
41
}
42
42
}
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
+ }
43
70
}
44
71