-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeetingRoom.java
More file actions
83 lines (73 loc) · 2.41 KB
/
MeetingRoom.java
File metadata and controls
83 lines (73 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package codeTop;
import java.util.Arrays;
import java.util.PriorityQueue;
/**
* codeTop中的会议室题目,分为会议室I与会议室II两个题目
* 一、会议室I:LeetCode 252
* 给定一个会议时间安排的数组,每个会议时间都会包括开始和结束的时间 [[s1,e1],[s2,e2],…] (si < ei),请你判断一个人是否能够参加这里面的全部会议。
*
* 示例 1:
* 输入: [[0,30],[5,10],[15,20]]
* 输出: false
*
* 示例 2:
* 输入: [[7,10],[2,4]]
* 输出: true
*
* 二、会议室II:leetCode 253
*给定一个会议时间安排的数组,每个会议时间都会包括开始和结束的时间 [[s1,e1],[s2,e2],…] (si < ei),为避免会议冲突,同时要考虑充分利用会议室资源,请你计算至少需要多少间会议室,才能满足这些会议安排。
*
* 示例 1:
* 输入: [[0, 30],[5, 10],[15, 20]]
* 输出: 2
*
* 示例 2:
* 输入: [[7,10],[2,4]]
* 输出: 1
*/
public class MeetingRoom {
public static void main(String[] args) {
}
/**
* 会议室I
* @param matrix
* @return
*/
public static boolean meetingRoom1(int[][] matrix){
if(matrix == null || matrix.length < 2){
return true;
}
Arrays.sort(matrix,(a,b) ->{return a[0] - b[0];});
int last = matrix[0][1];
for(int i = 1;i < matrix.length;i++){
if(last > matrix[i][0]){
return false;
}
last = matrix[i][1];
}
return true;
}
/**
* 会议室II
* @param matrix
* @return
*/
public static int meetingRoom(int[][] matrix){
if(matrix == null || matrix.length == 0){
return 0;
}
Arrays.sort(matrix,(a,b) ->{return a[0] - b[0];});
//保存当前正在开会的结束时间,queue中元素的个数即表示当前需要的会议室的数量
PriorityQueue<Integer> queue = new PriorityQueue<>();
int ans = 0;
for(int[] arr : matrix){
//当前要开的会是arr,如果当前要开的会的开始时间大于正在开的会的结束时间,则表示正在开的会已经结束,可以和将要开的会公用一个会议室
while(!queue.isEmpty() && queue.peek() <= arr[0]){
queue.poll();
}
queue.add(arr[1]);
ans = Math.max(ans,queue.size());
}
return ans;
}
}