-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMyCalendarThree.java
50 lines (39 loc) · 1.3 KB
/
MyCalendarThree.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
package leetcode.algo.misc.leet_zh_732;
import java.util.TreeMap;
public class MyCalendarThree {
/*
* 732. My Calendar III
* 执行用时 : 353 ms
* 内存消耗 : 55.5 MB
* */
private TreeMap<Integer, Integer> calendar;
public MyCalendarThree() {
calendar = new TreeMap<>();
}
public int book(int start, int end) {
// 添加至日程中
calendar.put(start, calendar.getOrDefault(start, 0) + 1);
calendar.put(end, calendar.getOrDefault(end, 0) - 1);
// 记录最大活跃的日程数
int max = 0;
// 记录活跃的日程数
int active = 0;
for (Integer d : calendar.values()) {
// 以时间线统计日程
active += d;
// 找到活跃事件数量最多的时刻,记录下来。
if (active > max) {
max = active;
}
}
return max;
}
public static void main(String[] args) {
MyCalendarThree myCalendar = new MyCalendarThree();
System.out.println(myCalendar.book(10, 20));
System.out.println(myCalendar.book(50, 60));
System.out.println(myCalendar.book(10, 40));
System.out.println(myCalendar.book(5, 15));
System.out.println(myCalendar.book(25, 55));
}
}