1 /** 2 * Definition for an interval. 3 * public class Interval { 4 * int start; 5 * int end; 6 * Interval() { start = 0; end = 0; } 7 * Interval(int s, int e) { start = s; end = e; } 8 * } 9 */ 10 public class Solution { 11 public int minMeetingRooms(Interval[] intervals) { 12 if (intervals.length < 2) { 13 return intervals.length; 14 } 15 16 Arrays.sort(intervals, new Comparator<Interval>() { 17 @Override 18 public int compare(Interval a, Interval b) { 19 return a.start - b.start; 20 } 21 }); 22 23 PriorityQueue<Interval> queue = new PriorityQueue<>(intervals.length, new Comparator<Interval>() { 24 @Override 25 public int compare(Interval a, Interval b) { 26 return a.end - b.end; 27 } 28 }); 29 30 queue.offer(intervals[0]); 31 for (int i = 1; i < intervals.length; i++) { 32 Interval current = queue.poll(); 33 if (intervals[i].start >= current.end) { 34 current.end = intervals[i].end; 35 } else { 36 queue.offer(intervals[i]); 37 } 38 queue.offer(current); 39 } 40 return queue.size(); 41 } 42 }
时间: 2024-10-13 06:24:30