Skip to content

Commit 8380f3f

Browse files
authoredJan 2, 2025··
feat: add solutions to lc problem: No.0729 (#3917)
No.0729.My Calendar I
1 parent 01db11b commit 8380f3f

File tree

9 files changed

+2673
-217
lines changed

9 files changed

+2673
-217
lines changed
 

‎solution/0700-0799/0729.My Calendar I/README.md

+895-72
Large diffs are not rendered by default.

‎solution/0700-0799/0729.My Calendar I/README_EN.md

+888-71
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
class MyCalendar {
22
public:
3-
map<int, int> m;
4-
53
MyCalendar() {
64
}
75

8-
bool book(int start, int end) {
9-
++m[start];
10-
--m[end];
11-
int s = 0;
12-
for (auto& [k, v] : m) {
13-
s += v;
14-
if (s > 1) {
15-
--m[start];
16-
++m[end];
17-
return false;
18-
}
6+
bool book(int startTime, int endTime) {
7+
auto e = m.lower_bound(startTime + 1);
8+
if (e != m.end() && e->second < endTime) {
9+
return false;
1910
}
11+
m[endTime] = startTime;
2012
return true;
2113
}
14+
15+
private:
16+
map<int, int> m;
2217
};
2318

2419
/**
2520
* Your MyCalendar object will be instantiated and called as such:
2621
* MyCalendar* obj = new MyCalendar();
27-
* bool param_1 = obj->book(start,end);
28-
*/
22+
* bool param_1 = obj->book(startTime,endTime);
23+
*/

‎solution/0700-0799/0729.My Calendar I/Solution.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,16 @@ func Constructor() MyCalendar {
88
}
99
}
1010

11-
func (this *MyCalendar) Book(start int, end int) bool {
12-
if p, ok := this.rbt.Floor(start); ok && p.Value.(int) > start {
11+
func (this *MyCalendar) Book(startTime int, endTime int) bool {
12+
if p, ok := this.rbt.Ceiling(startTime + 1); ok && p.Value.(int) < endTime {
1313
return false
1414
}
15-
if p, ok := this.rbt.Ceiling(start); ok && p.Key.(int) < end {
16-
return false
17-
}
18-
this.rbt.Put(start, end)
15+
this.rbt.Put(endTime, startTime)
1916
return true
2017
}
2118

2219
/**
2320
* Your MyCalendar object will be instantiated and called as such:
2421
* obj := Constructor();
25-
* param_1 := obj.Book(start,end);
26-
*/
22+
* param_1 := obj.Book(startTime,endTime);
23+
*/
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
1-
import java.util.Map;
2-
import java.util.TreeMap;
3-
41
class MyCalendar {
5-
62
private final TreeMap<Integer, Integer> tm = new TreeMap<>();
73

84
public MyCalendar() {
95
}
106

11-
public boolean book(int start, int end) {
12-
Map.Entry<Integer, Integer> ent = tm.floorEntry(start);
13-
if (ent != null && ent.getValue() > start) {
14-
return false;
15-
}
16-
ent = tm.ceilingEntry(start);
17-
if (ent != null && ent.getKey() < end) {
7+
public boolean book(int startTime, int endTime) {
8+
var e = tm.ceilingEntry(startTime + 1);
9+
if (e != null && e.getValue() < endTime) {
1810
return false;
1911
}
20-
tm.put(start, end);
12+
tm.put(endTime, startTime);
2113
return true;
2214
}
2315
}
2416

2517
/**
26-
* Your MyCalendar object will be instantiated and called as such: MyCalendar
27-
* obj = new MyCalendar(); boolean param_1 = obj.book(start,end);
28-
*/
18+
* Your MyCalendar object will be instantiated and called as such:
19+
* MyCalendar obj = new MyCalendar();
20+
* boolean param_1 = obj.book(startTime,endTime);
21+
*/

0 commit comments

Comments
 (0)
Please sign in to comment.