Leetcode 253: meeting room

Arrang classes, you’re gonna figure out the min classroom required.

input: [0, 30],[5, 10],[15, 20]
output: 2

Java:



/*
 * public class Interval {
	int start;
	int end;
	
	public Interval() {
		this.start = 0;
		this.end = 0;
	}
		
	public Interval(int s, int e) {
		this.start = s;
		this.end = e;
	}	
}
 */



class Solution {
	
	
	public static int minMeetingRooms(Interval[] intervals) {

		Arrays.sort(intervals, new Comparator<Interval>(){
			@Override
			public int compare(Interval i1, Interval i2) {
				return i1.start - i2.start;
			}
		});
		
		int room = 0;
		PriorityQueue<Interval> minHeap = new PriorityQueue<Interval>((Interval i1, Interval i2) -> i1.end - i2.end);
		for (Interval temp:intervals) {
			minHeap.offer(temp);
			if (temp.start < minHeap.peek().end) room++;
			else minHeap.poll();
		}
		return room;
	}
	
	
	public static void main(String[] args) {
		
		Interval[] intervals = {new Interval(0, 30), new Interval(5, 10), new Interval(15, 20)};	
		System.out.println(minMeetingRooms(intervals));	
		
	}  
	
}

另一个骚气的方法,将class 开始的时间map成+1, 结束的时间map成-1; 最后遍历,和最大的时候就是min room


class Solution {

	public static int minMeetingRooms(Interval[] intervals) {
	
		Arrays.sort(intervals, ((Interval i1, Interval i2) -> i2.end - i1.end));
		int[] map = new int[intervals[0].end+1];
		
		for (Interval temp:intervals) {
			map[temp.start]++;
			map[temp.end]--;
		}
		
		int sum = 0, max = 0;
		for (int i=0; i<map.length; i++) {
			
			sum += map[i];
			max = Math.max(sum, max);
			
		}
	
		return max;
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值