-
Notifications
You must be signed in to change notification settings - Fork 112
/
Solution.java
44 lines (34 loc) · 1.06 KB
/
Solution.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
import java.util.Map;
import java.util.TreeMap;
/**
* @author Oleg Cherednik
* @since 30.01.2019
*/
public class Solution {
public static void main(String... args) {
System.out.println(getRequiredRoomsAmount(new Interval[]{
new Interval(30, 75),
new Interval(0, 50),
new Interval(60, 150)}));
}
private static final class Interval {
private final int start;
private final int end;
public Interval(int start, int end) {
this.start = start;
this.end = end;
}
}
public static int getRequiredRoomsAmount(Interval[] intervals) {
Map<Integer, Integer> map = new TreeMap<>();
for (Interval interval : intervals) {
map.put(interval.start, map.getOrDefault(interval.start, 0) + 1);
map.put(interval.end, map.getOrDefault(interval.end, 0) - 1);
}
int res = 0;
int count = 0;
for (int delta : map.values())
res = Math.max(res, count += delta);
return res;
}
}