-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path253.Meeting_Rooms_II.java
89 lines (81 loc) · 2.22 KB
/
253.Meeting_Rooms_II.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
/**
* Definition of Interval:
* public class Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
* }
*/
public class Solution {
/**
* @param intervals: an array of meeting time intervals
* @return: the minimum number of conference rooms required
*/
public int minMeetingRooms(List<Interval> intervals) {
if (intervals == null || intervals.size() == 0) {
return 0;
}
// Write your code here
Map<Integer, Integer> map = new TreeMap<>(); // to keep the keys sorted ASC
for (Interval iv : intervals) {
map.putIfAbsent(iv.start, 0);
map.computeIfPresent(iv.start, (k,v) -> v+1);
map.putIfAbsent(iv.end, 0);
map.computeIfPresent(iv.end, (k,v) -> v-1);
}
List<Integer> keys = new ArrayList<>(map.keySet());
int max = 0;
int curr = 0;
for (int k : keys) {
curr += map.get(k);
max = Math.max(max, curr);
}
return max;
}
}
/**
* Two Pointer
*/
public class Solution {
/**
* @param intervals: an array of meeting time intervals
* @return: the minimum number of conference rooms required
*/
public int minMeetingRooms(List<Interval> intervals) {
if (intervals == null || intervals.size() == 0) {
return 0;
}
int n = intervals.size();
// Write your code here
int[] start = new int[n];
int[] end = new int[n];
for (int i=0; i<n; i++) {
Interval iv = intervals.get(i);
start[i] = iv.start;
end[i] = iv.end;
}
Arrays.sort(start);
Arrays.sort(end);
int s=0;
int e=0;
int curr = 0;
int max = 0;
while (s<n && e<n) {
if (start[s] < end[e]) {
curr++;
s++;
max = Math.max(max, curr);
} else if (start[s] > end[e]) {
curr--;
e++;
max = Math.max(max, curr);
} else {
s++;
e++;
}
}
return max;
}
}