|
| 1 | +import java.util.List; |
| 2 | +import java.util.PriorityQueue; |
| 3 | + |
| 4 | +/** |
| 5 | + * Definition of Interval: |
| 6 | + * public class Interval { |
| 7 | + * int start, end; |
| 8 | + * Interval(int start, int end) { |
| 9 | + * this.start = start; |
| 10 | + * this.end = end; |
| 11 | + * } |
| 12 | + * } |
| 13 | + */ |
| 14 | + |
| 15 | +public class Solution { |
| 16 | + /** |
| 17 | + * @param intervals: an array of meeting time intervals |
| 18 | + * @return: the minimum number of conference rooms required |
| 19 | + */ |
| 20 | + // ์๊ฐ๋ณต์ก๋: O(n log n) |
| 21 | + public int minMeetingRooms(List<Interval> intervals) { |
| 22 | + |
| 23 | + if (intervals == null || intervals.isEmpty()) { |
| 24 | + return 0; |
| 25 | + } |
| 26 | + |
| 27 | + // ์ฃผ์ด์ง ์๊ฐ ์ธํฐ๋ฒ ๋ฆฌ์คํธ๋ฅผ ์์ ์๊ฐ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ |
| 28 | + intervals.sort((i1, i2) -> i1.start - i2.start); |
| 29 | + |
| 30 | + // PriorityQueue ์ ์ธ: ์๊ฐ interval์ end time ๊ธฐ์ค |
| 31 | + PriorityQueue<Interval> meetingRooms = new PriorityQueue<>((i1, i2) -> i1.end - i2.end); |
| 32 | + |
| 33 | + for (Interval interval : intervals) { |
| 34 | + |
| 35 | + // ์ต์ด meeting room |
| 36 | + if (meetingRooms.isEmpty()) { |
| 37 | + meetingRooms.offer(interval); |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + Interval meeting = meetingRooms.peek(); |
| 42 | + // (0, 8), (8, 10) is not conflict at 8 |
| 43 | + if (meeting.end <= interval.start) { |
| 44 | + // ์์ ํ์ ์ข
๋ฃ๋์์ผ๋ฏ๋ก ํ์์ค ์ฌ์ฌ์ฉ |
| 45 | + meetingRooms.poll(); |
| 46 | + } |
| 47 | + // ์ ํ์์ค ์ถ๊ฐ |
| 48 | + meetingRooms.offer(interval); |
| 49 | + } |
| 50 | + |
| 51 | + return meetingRooms.size(); |
| 52 | + |
| 53 | + } |
| 54 | +} |
| 55 | + |
0 commit comments