欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

leetcode_253_会议室 II

程序员文章站 2022-06-03 09:56:59
...

leetcode_253_会议室 II

class Solution {
    	public int minMeetingRooms(int[][] intervals) {
		if (intervals == null || intervals.length == 0)
			return 0;
		PriorityQueue<Integer> heap = new PriorityQueue<Integer>();

		Arrays.sort(intervals, (o1, o2) -> o1[0] - o2[0]);
		heap.add(intervals[0][1]);
		for (int i = 1; i < intervals.length; i++) {
			if (heap.peek() <= intervals[i][0]) {
				heap.remove();
			}
			heap.add(intervals[i][1]);
		}
		return heap.size();
	}
}
class Solution {
   	public int minMeetingRooms(int[][] intervals) {
		if (intervals == null || intervals.length == 0)
			return 0;
		int[] starts = new int[intervals.length];
		int[] ends = new int[intervals.length];
		for (int i = 0; i < intervals.length; i++) {
			starts[i] = intervals[i][0];
			ends[i] = intervals[i][1];
		}
		Arrays.sort(starts);
		Arrays.sort(ends);
		int count = 0;
		int endIdx = 0;
		for (int start : starts) {
			if (start >= ends[endIdx]) {
				endIdx++;
			} else {
				count++;
			}
		}
		return count;
	}
}
相关标签: 数据结构与算法