-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMyCalendarTwo.java
99 lines (79 loc) · 3.42 KB
/
MyCalendarTwo.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
99
package leetcode.algo.misc.leet_zh_731;
import java.util.TreeMap;
public class MyCalendarTwo {
/*
* 731. My Calendar II
* 执行用时 : 611 ms
* 内存消耗 : 56.4 MB
*
* When booking a new event [start, end), count delta[start]++ and delta[end]--.
* When processing the values of delta in sorted order of their keys,
* the running sum active is the number of events open at that time.
* If the sum is 3 or more, that time is (at least) triple booked.
*
*
* */
private TreeMap<Integer, Integer> calendar;
public MyCalendarTwo() {
calendar = new TreeMap<>();
}
public boolean book(int start, int end) {
// 尝试添加至日程中
calendar.put(start, calendar.getOrDefault(start, 0) + 1);
calendar.put(end, calendar.getOrDefault(end, 0) - 1);
// 记录活跃的日程数
int active = 0;
for (int d : calendar.values()) {
// 以时间线统计日程
active += d;
// 中途活跃日程>=3时,返回 false
if (active >= 3) {
// 恢复现场
calendar.put(start, calendar.get(start) - 1);
calendar.put(end, calendar.get(end) + 1);
// remove this part, it can passes. but this will only costs more spaces.
if (calendar.get(start) == 0)
calendar.remove(start);
return false;
}
}
return true;
}
public static void main(String[] args) {
MyCalendarTwo myCalendar = new MyCalendarTwo();
// System.out.println(myCalendar.book(10, 20));
// System.out.println(myCalendar.book(15, 25));
// System.out.println(myCalendar.book(20, 30));
// System.out.println(myCalendar.book(37, 50));
// System.out.println(myCalendar.book(33, 50));
// System.out.println(myCalendar.book(4, 17));
// System.out.println(myCalendar.book(35, 48));
// System.out.println(myCalendar.book(8, 25));
// System.out.println(myCalendar.book(47, 50));
// System.out.println(myCalendar.book(33, 41));
// System.out.println(myCalendar.book(39, 45));
// System.out.println(myCalendar.book(33, 42));
// System.out.println(myCalendar.book(25, 32));
// System.out.println(myCalendar.book(26, 35));
// System.out.println(myCalendar.book(19, 25));
// System.out.println(myCalendar.book(3, 8));
// System.out.println(myCalendar.book(8, 13));
// System.out.println(myCalendar.book(18, 27));
// System.out.println(myCalendar.book(20, 29));
// System.out.println(myCalendar.book(13, 22));
// System.out.println(myCalendar.book(44, 50));
// System.out.println(myCalendar.book(1, 7));
// System.out.println(myCalendar.book(2, 10));
// System.out.println(myCalendar.book(14, 20));
// System.out.println(myCalendar.book(19, 25));
// System.out.println(myCalendar.book(36, 42));
// System.out.println(myCalendar.book(45, 50));
// System.out.println(myCalendar.book(47, 50));
// System.out.println(myCalendar.book(39, 45));
// System.out.println(myCalendar.book(44, 50));
// System.out.println(myCalendar.book(16, 25));
// List<Integer> list = new ArrayList<>();
// list.add(1, 1); // (<tt>index < 0 || index >= size()</tt>)
// System.out.println(list);
}
}