My Calendar III LeetCode solution in Java

1
class MyCalendarThree {
2

3
TreeMap<Integer, Integer> map;
4

5
public MyCalendarThree() {
6
map = new TreeMap<>();
7
}
8

9
public int book(int start, int end) {
10
if (map.isEmpty()) {
11
map.put(start, 1);
12
map.put(end, -1);
13
return 1;
14
}
15

16
// upvote if you like the solution
17

18
map.put(start, map.getOrDefault(start, 0) + 1);
19
map.put(end, map.getOrDefault(end, 0) - 1);
20

21
int res = 0;
22
int sum = 0;
23
for (Map.Entry<Integer, Integer> e : map.entrySet()) {
24
sum += e.getValue();
25
res = Math.max(res, sum);
26
}
27

28
return res;
29
}
30
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0