File tree 9 files changed +2673
-217
lines changed
solution/0700-0799/0729.My Calendar I
9 files changed +2673
-217
lines changed Load Diff Large diffs are not rendered by default.
Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change 1
1
class MyCalendar {
2
2
public:
3
- map<int , int > m;
4
-
5
3
MyCalendar () {
6
4
}
7
5
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 ;
19
10
}
11
+ m[endTime] = startTime;
20
12
return true ;
21
13
}
14
+
15
+ private:
16
+ map<int , int > m;
22
17
};
23
18
24
19
/* *
25
20
* Your MyCalendar object will be instantiated and called as such:
26
21
* MyCalendar* obj = new MyCalendar();
27
- * bool param_1 = obj->book(start,end );
28
- */
22
+ * bool param_1 = obj->book(startTime,endTime );
23
+ */
Original file line number Diff line number Diff line change @@ -8,19 +8,16 @@ func Constructor() MyCalendar {
8
8
}
9
9
}
10
10
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 {
13
13
return false
14
14
}
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 )
19
16
return true
20
17
}
21
18
22
19
/**
23
20
* Your MyCalendar object will be instantiated and called as such:
24
21
* obj := Constructor();
25
- * param_1 := obj.Book(start,end );
26
- */
22
+ * param_1 := obj.Book(startTime,endTime );
23
+ */
Original file line number Diff line number Diff line change 1
- import java .util .Map ;
2
- import java .util .TreeMap ;
3
-
4
1
class MyCalendar {
5
-
6
2
private final TreeMap <Integer , Integer > tm = new TreeMap <>();
7
3
8
4
public MyCalendar () {
9
5
}
10
6
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 ) {
18
10
return false ;
19
11
}
20
- tm .put (start , end );
12
+ tm .put (endTime , startTime );
21
13
return true ;
22
14
}
23
15
}
24
16
25
17
/**
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
+ */
You can’t perform that action at this time.
0 commit comments