Skip to content

Commit 4557f41

Browse files
update 253
1 parent 494db6f commit 4557f41

File tree

1 file changed

+7
-44
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+7
-44
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_253.java

+7-44
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,30 @@
55

66
public class _253 {
77
public static class Solution1 {
8-
98
public int minMeetingRooms(int[][] intervals) {
109
if (intervals == null || intervals.length == 0) {
1110
return 0;
1211
}
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
2315
for (int i = 1; i < intervals.length; i++) {
2416
// 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]) {
2819
// if the current meeting starts right after
2920
// there's no need for a new room, merge the interval
30-
interval[1] = intervals[i][1];
21+
last[1] = intervals[i][1];
3122
} else {
3223
// otherwise, this meeting needs a new room
3324
heap.offer(intervals[i]);
3425
}
35-
3626
// don't forget to put the meeting room back
37-
heap.offer(interval);
27+
heap.offer(last);
3828
}
3929

4030
return heap.size();
4131
}
4232
}
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-
}
7033
}
7134

0 commit comments

Comments
 (0)