Skip to content

Commit 6e7371a

Browse files
author
Thuy Trinh
committed
Solution for "Meeting Rooms II"
1 parent a3bdb61 commit 6e7371a

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
buildscript {
2-
ext.kotlinVersion = '1.1.51'
2+
ext.kotlinVersion = '1.1.60'
33

44
repositories {
55
mavenCentral()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
i0: [1,5]
2+
i1: [2,3]
3+
i3: [3,5]
4+
i4: [7,8]
5+
6+
Given a list of rooms, and a given event,
7+
which room we should add to achieve min count of rooms?
8+
Choose the room having earliest finish. However, if the event overlaps with the
9+
room, that means it can't fit the other rooms. So, new room should be created.
10+
11+
If we have a list of events, which event we should choose to achieve min count
12+
of rooms? With a room having earliest finish, the gap between room finish time
13+
and the event's start time should be minimal. That means, we should select the
14+
event having earliest start time.
15+
16+
Solution:
17+
18+
* Sort by earliest start
19+
* For each event and given a room list:
20+
* Select a room having earliest finish
21+
* If we can't add this event into the selected room,
22+
that means we can't add it into other rooms.
23+
So create a new room for this event.
24+
* Otherwise, add it into the selected room.
25+
Also, we'll have to change the finish time of this room.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package leetcode.meetingrooms2
2+
3+
import leetcode.meetingrooms.Interval
4+
import java.util.*
5+
6+
typealias RoomEndTime = Int
7+
typealias Rooms = PriorityQueue<RoomEndTime>
8+
9+
/**
10+
* https://leetcode.com/problems/meeting-rooms-ii/description/
11+
*/
12+
class Solution {
13+
private fun RoomEndTime.hasNoOverlap(i: Interval): Boolean = this <= i.start
14+
15+
fun minMeetingRooms(intervals: Array<Interval>): Int {
16+
if (intervals.isEmpty()) {
17+
return 0
18+
}
19+
20+
intervals.sortBy { it.start }
21+
val rooms = Rooms()
22+
intervals.forEach {
23+
when {
24+
rooms.isNotEmpty() && rooms.peek().hasNoOverlap(it) -> {
25+
rooms.poll()
26+
rooms.offer(it.end)
27+
}
28+
else -> rooms.offer(it.end)
29+
}
30+
}
31+
return rooms.size
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package leetcode.meetingrooms2
2+
3+
import leetcode.meetingrooms.Interval
4+
import org.amshove.kluent.shouldEqualTo
5+
import org.junit.Test
6+
7+
class SolutionTest {
8+
@Test
9+
fun test() {
10+
Solution().minMeetingRooms(arrayOf(
11+
Interval(1, 5),
12+
Interval(2, 3),
13+
Interval(3, 5),
14+
Interval(7, 8)
15+
)).shouldEqualTo(2)
16+
Solution().minMeetingRooms(arrayOf(
17+
Interval(2, 15),
18+
Interval(36, 45),
19+
Interval(9, 29),
20+
Interval(16, 23),
21+
Interval(4, 9)
22+
)).shouldEqualTo(2)
23+
}
24+
}

0 commit comments

Comments
 (0)