-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeeitngRooms.java
98 lines (79 loc) · 2.44 KB
/
MeeitngRooms.java
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
Problem Description
Given an 2D integer array A of size N x 2 denoting time intervals of different meetings.
Where:
A[i][0] = start time of the ith meeting.
A[i][1] = end time of the ith meeting.
Find the minimum number of conference rooms required so that all meetings can be done.
Problem Constraints
1 <= N <= 10
0 <= A[i][0] < A[i][1] <= 2 * 109
Input Format
The only argument given is the matrix A.
Output Format
Return the minimum number of conference rooms required so that all meetings can be done.
Example Input
Input 1:
A = [ [0, 30]
[5, 10]
[15, 20]
]
Input 2:
A = [ [1, 18]
[18, 23]
[15, 29]
[4, 15]
[2, 11]
[5, 13]
]
Example Output
Output 1:
2
Output 2:
4
Example Explanation
Explanation 1:
Meeting one can be done in conference room 1 form 0 - 30.
Meeting two can be done in conference room 2 form 5 - 10.
Meeting one can be done in conference room 2 form 15 - 20 as it is free in this interval.
Explanation 2:
Meeting one can be done in conference room 1 from 1 - 18.
Meeting five can be done in conference room 2 from 2 - 11.
Meeting four can be done in conference room 3 from 4 - 15.
Meeting six can be done in conference room 4 from 5 - 13.
Meeting three can be done in conference room 2 from 15 - 29 as it is free in this interval.
Meeting two can be done in conference room 4 from 18 - 23 as it is free in this interval.
*/
class Meeting implements Comparable<Meeting>{
int start;
int end;
public Meeting(int start,int end){
this.start = start;
this.end = end;
}
public int compareTo(Meeting m){
return this.start - m.start;
}
}
public class Solution {
public int solve(ArrayList<ArrayList<Integer>> A) {
ArrayList<Meeting> meeting = new ArrayList<>();
for(int i=0; i<A.size(); i++){
meeting.add(new Meeting(A.get(i).get(0),A.get(i).get(1)));
}
Collections.sort(meeting);
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
for(int i=0; i<meeting.size(); i++){
if(minHeap.size() > 0){
if(meeting.get(i).start >= minHeap.peek()){
minHeap.poll();
}
minHeap.add(meeting.get(i).end);
}
else{
minHeap.add(meeting.get(i).end);
}
}
return minHeap.size();
}
}