Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3a2a84e

Browse files
refactor 253
1 parent69ae9dc commit3a2a84e

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
returnheap.size();
4141
}
4242
}
43+
44+
publicstaticclassSolution2 {
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+
publicintminMeetingRooms(int[][]intervals) {
50+
//I use the meeting's end time as the room indicate and put them into a heap
51+
PriorityQueue<Integer>rooms =newPriorityQueue<>();
52+
Arrays.sort(intervals, (a,b) ->a[0] -b[0]);
53+
for (inti =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+
returnrooms.size();
68+
}
69+
}
4370
}
4471

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp