Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 12d41e7

Browse files
feat: add solutions to lc/lcof2 problems
* Add solutions to lcof2 problem: No.058 * Add solutions to lc problem: No.0729.My Calendar I
1 parent efb1ee4 commit 12d41e7

File tree

9 files changed

+363
-30
lines changed

9 files changed

+363
-30
lines changed

‎lcof2/剑指 Offer II 058. 日程表/README.md‎

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
</strong>[&quot;MyCalendar&quot;,&quot;book&quot;,&quot;book&quot;,&quot;book&quot;]
2424
[[],[10,20],[15,25],[20,30]]
2525
<strong>输出:</strong> [null,true,false,true]
26-
<strong>解释:</strong>
26+
<strong>解释:</strong>
2727
MyCalendar myCalendar = new MyCalendar();
28-
MyCalendar.book(10, 20); // returns true
28+
MyCalendar.book(10, 20); // returns true
2929
MyCalendar.book(15, 25); // returns false ,第二个日程安排不能添加到日历中,因为时间 15 已经被第一个日程安排预定了
30-
MyCalendar.book(20, 30); // returns true ,第三个日程安排可以添加到日历中,因为第一个日程安排并不包含时间 20
30+
MyCalendar.book(20, 30); // returns true ,第三个日程安排可以添加到日历中,因为第一个日程安排并不包含时间 20
3131
</pre>
3232

3333
<p>&nbsp;</p>
@@ -57,15 +57,93 @@ MyCalendar.book(20, 30); // returns true ,第三个日程安排可以添加到
5757
<!-- 这里可写当前语言的特殊实现逻辑 -->
5858

5959
```python
60+
from sortedcontainers import SortedDict
6061

62+
63+
class MyCalendar:
64+
65+
def __init__(self):
66+
self.sd = SortedDict()
67+
68+
def book(self, start: int, end: int) -> bool:
69+
idx = self.sd.bisect_right(start)
70+
if 0 <= idx < len(self.sd):
71+
if end > self.sd.values()[idx]:
72+
return False
73+
self.sd[end] = start
74+
return True
75+
76+
77+
# Your MyCalendar object will be instantiated and called as such:
78+
# obj = MyCalendar()
79+
# param_1 = obj.book(start,end)
6180
```
6281

6382
### **Java**
6483

6584
<!-- 这里可写当前语言的特殊实现逻辑 -->
6685

6786
```java
87+
import java.util.Map;
88+
import java.util.TreeMap;
89+
90+
class MyCalendar {
91+
92+
private final TreeMap<Integer, Integer> tm = new TreeMap<>();
93+
94+
public MyCalendar() {
95+
}
96+
97+
public boolean book(int start, int end) {
98+
Map.Entry<Integer, Integer> ent = tm.floorEntry(start);
99+
if (ent != null && ent.getValue() > start) {
100+
return false;
101+
}
102+
ent = tm.ceilingEntry(start);
103+
if (ent != null && ent.getKey() < end) {
104+
return false;
105+
}
106+
tm.put(start, end);
107+
return true;
108+
}
109+
}
110+
111+
/**
112+
* Your MyCalendar object will be instantiated and called as such: MyCalendar
113+
* obj = new MyCalendar(); boolean param_1 = obj.book(start,end);
114+
*/
115+
```
68116

117+
### **Go**
118+
119+
```go
120+
type MyCalendar struct {
121+
rbt *redblacktree.Tree
122+
}
123+
124+
func Constructor() MyCalendar {
125+
return MyCalendar{
126+
rbt: redblacktree.NewWithIntComparator(),
127+
}
128+
}
129+
130+
func (this *MyCalendar) Book(start int, end int) bool {
131+
if p, ok := this.rbt.Floor(start); ok && p.Value.(int) > start {
132+
return false
133+
}
134+
if p, ok := this.rbt.Ceiling(start); ok && p.Key.(int) < end {
135+
return false
136+
}
137+
this.rbt.Put(start, end)
138+
return true
139+
}
140+
141+
142+
/**
143+
* Your MyCalendar object will be instantiated and called as such:
144+
* obj := Constructor();
145+
* param_1 := obj.Book(start,end);
146+
*/
69147
```
70148

71149
### **...**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
type MyCalendar struct {
2+
rbt *redblacktree.Tree
3+
}
4+
5+
func Constructor() MyCalendar {
6+
return MyCalendar{
7+
rbt: redblacktree.NewWithIntComparator(),
8+
}
9+
}
10+
11+
func (this *MyCalendar) Book(start int, end int) bool {
12+
if p, ok := this.rbt.Floor(start); ok && p.Value.(int) > start {
13+
return false
14+
}
15+
if p, ok := this.rbt.Ceiling(start); ok && p.Key.(int) < end {
16+
return false
17+
}
18+
this.rbt.Put(start, end)
19+
return true
20+
}
21+
22+
/**
23+
* Your MyCalendar object will be instantiated and called as such:
24+
* obj := Constructor();
25+
* param_1 := obj.Book(start,end);
26+
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.Map;
2+
import java.util.TreeMap;
3+
4+
class MyCalendar {
5+
6+
private final TreeMap<Integer, Integer> tm = new TreeMap<>();
7+
8+
public MyCalendar() {
9+
}
10+
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) {
18+
return false;
19+
}
20+
tm.put(start, end);
21+
return true;
22+
}
23+
}
24+
25+
/**
26+
* Your MyCalendar object will be instantiated and called as such: MyCalendar
27+
* obj = new MyCalendar(); boolean param_1 = obj.book(start,end);
28+
*/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from sortedcontainers import SortedDict
2+
3+
4+
class MyCalendar:
5+
6+
def __init__(self):
7+
self.sd = SortedDict()
8+
9+
def book(self, start: int, end: int) -> bool:
10+
idx = self.sd.bisect_right(start)
11+
if 0 <= idx < len(self.sd):
12+
if end > self.sd.values()[idx]:
13+
return False
14+
self.sd[end] = start
15+
return True
16+
17+
18+
# Your MyCalendar object will be instantiated and called as such:
19+
# obj = MyCalendar()
20+
# param_1 = obj.book(start,end)

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

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ MyCalendar();
2323
MyCalendar.book(10, 20); // returns true
2424
MyCalendar.book(15, 25); // returns false
2525
MyCalendar.book(20, 30); // returns true
26-
<strong>解释:</strong>
26+
<strong>解释:</strong>
2727
第一个日程安排可以添加到日历中. 第二个日程安排不能添加到日历中,因为时间 15 已经被第一个日程安排预定了。
2828
第三个日程安排可以添加到日历中,因为第一个日程安排并不包含时间 20 。
2929
</pre>
@@ -47,31 +47,93 @@ MyCalendar.book(20, 30); // returns true
4747
<!-- 这里可写当前语言的特殊实现逻辑 -->
4848

4949
```python
50+
from sortedcontainers import SortedDict
5051

52+
53+
class MyCalendar:
54+
55+
def __init__(self):
56+
self.sd = SortedDict()
57+
58+
def book(self, start: int, end: int) -> bool:
59+
idx = self.sd.bisect_right(start)
60+
if 0 <= idx < len(self.sd):
61+
if end > self.sd.values()[idx]:
62+
return False
63+
self.sd[end] = start
64+
return True
65+
66+
67+
# Your MyCalendar object will be instantiated and called as such:
68+
# obj = MyCalendar()
69+
# param_1 = obj.book(start,end)
5170
```
5271

5372
### **Java**
5473

5574
<!-- 这里可写当前语言的特殊实现逻辑 -->
5675

5776
```java
77+
import java.util.Map;
78+
import java.util.TreeMap;
79+
5880
class MyCalendar {
59-
List<int[]> calendar;
6081

61-
MyCalendar() {
62-
calendar = new ArrayList<>();
82+
private final TreeMap<Integer, Integer> tm = new TreeMap<>();
83+
84+
public MyCalendar() {
6385
}
6486

6587
public boolean book(int start, int end) {
66-
for (int[] item : calendar) {
67-
if (item[0] < end && item[1] > start) {
68-
return false;
69-
}
88+
Map.Entry<Integer, Integer> ent = tm.floorEntry(start);
89+
if (ent != null && ent.getValue() > start) {
90+
return false;
91+
}
92+
ent = tm.ceilingEntry(start);
93+
if (ent != null && ent.getKey() < end) {
94+
return false;
7095
}
71-
calendar.add(newint[]{start, end});
96+
tm.put(start, end);
7297
return true;
7398
}
7499
}
100+
101+
/**
102+
* Your MyCalendar object will be instantiated and called as such: MyCalendar
103+
* obj = new MyCalendar(); boolean param_1 = obj.book(start,end);
104+
*/
105+
```
106+
107+
### **Go**
108+
109+
```go
110+
type MyCalendar struct {
111+
rbt *redblacktree.Tree
112+
}
113+
114+
func Constructor() MyCalendar {
115+
return MyCalendar{
116+
rbt: redblacktree.NewWithIntComparator(),
117+
}
118+
}
119+
120+
func (this *MyCalendar) Book(start int, end int) bool {
121+
if p, ok := this.rbt.Floor(start); ok && p.Value.(int) > start {
122+
return false
123+
}
124+
if p, ok := this.rbt.Ceiling(start); ok && p.Key.(int) < end {
125+
return false
126+
}
127+
this.rbt.Put(start, end)
128+
return true
129+
}
130+
131+
132+
/**
133+
* Your MyCalendar object will be instantiated and called as such:
134+
* obj := Constructor();
135+
* param_1 := obj.Book(start,end);
136+
*/
75137
```
76138

77139
### **...**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /