Skip to content

Commit 3a2a84e

Browse files
refactor 253
1 parent 69ae9dc commit 3a2a84e

File tree

1 file changed

+27
-0
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+27
-0
lines changed

src/main/java/com/fishercoder/solutions/_253.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,32 @@ public int minMeetingRooms(int[][] intervals) {
4040
return heap.size();
4141
}
4242
}
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+
}
4370
}
4471

0 commit comments

Comments
 (0)