Skip to content

Commit c053623

Browse files
committed
feat: add solutions to lc problem: No.0731
No.0731.My Calendar II
1 parent 0d8ecb4 commit c053623

File tree

5 files changed

+239
-42
lines changed

5 files changed

+239
-42
lines changed

solution/0700-0799/0731.My Calendar II/README.md

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,34 @@ MyCalendar.book(25, 55); // returns true
5555
<!-- 这里可写当前语言的特殊实现逻辑 -->
5656

5757
```python
58-
58+
from sortedcontainers import SortedDict
59+
60+
61+
class MyCalendarTwo:
62+
63+
def __init__(self):
64+
self.sd = SortedDict()
65+
66+
def book(self, start: int, end: int) -> bool:
67+
if start not in self.sd:
68+
self.sd[start] = 0
69+
if end not in self.sd:
70+
self.sd[end] = 0
71+
self.sd[start] += 1
72+
self.sd[end] -= 1
73+
s = 0
74+
for v in self.sd.values():
75+
s += v
76+
if s > 2:
77+
self.sd[start] -= 1
78+
self.sd[end] += 1
79+
return False
80+
return True
81+
82+
83+
# Your MyCalendarTwo object will be instantiated and called as such:
84+
# obj = MyCalendarTwo()
85+
# param_1 = obj.book(start,end)
5986
```
6087

6188
### **Java**
@@ -64,29 +91,69 @@ MyCalendar.book(25, 55); // returns true
6491

6592
```java
6693
class MyCalendarTwo {
67-
List<int[]> calendar;
68-
List<int[]> duplicationList;
94+
private Map<Integer, Integer> tm = new TreeMap<>();
6995

70-
MyCalendarTwo() {
71-
calendar = new ArrayList<>();
72-
duplicationList = new ArrayList<>();
73-
}
96+
public MyCalendarTwo() {
7497

98+
}
99+
75100
public boolean book(int start, int end) {
76-
for (int[] item : duplicationList) {
77-
if (item[0] < end && item[1] > start) {
101+
tm.put(start, tm.getOrDefault(start, 0) + 1);
102+
tm.put(end, tm.getOrDefault(end, 0) - 1);
103+
int s = 0;
104+
for (int v : tm.values()) {
105+
s += v;
106+
if (s > 2) {
107+
tm.put(start, tm.get(start) - 1);
108+
tm.put(end, tm.get(end) + 1);
78109
return false;
79110
}
80111
}
81-
for (int[] item : calendar) {
82-
if (item[0] < end && item[1] > start) {
83-
duplicationList.add(new int[]{Math.max(start, item[0]), Math.min(end, item[1])});
112+
return true;
113+
}
114+
}
115+
116+
/**
117+
* Your MyCalendarTwo object will be instantiated and called as such:
118+
* MyCalendarTwo obj = new MyCalendarTwo();
119+
* boolean param_1 = obj.book(start,end);
120+
*/
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
class MyCalendarTwo {
127+
public:
128+
map<int, int> m;
129+
130+
MyCalendarTwo() {
131+
132+
}
133+
134+
bool book(int start, int end) {
135+
++m[start];
136+
--m[end];
137+
int s = 0;
138+
for (auto& [k, v] : m)
139+
{
140+
s += v;
141+
if (s > 2)
142+
{
143+
--m[start];
144+
++m[end];
145+
return false;
84146
}
85147
}
86-
calendar.add(new int[]{start, end});
87148
return true;
88149
}
89-
}
150+
};
151+
152+
/**
153+
* Your MyCalendarTwo object will be instantiated and called as such:
154+
* MyCalendarTwo* obj = new MyCalendarTwo();
155+
* bool param_1 = obj->book(start,end);
156+
*/
90157
```
91158
92159
### **...**

solution/0700-0799/0731.My Calendar II/README_EN.md

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,36 +52,103 @@ myCalendarTwo.book(25, 55); // return True, The event can be booked, as the time
5252
### **Python3**
5353

5454
```python
55-
55+
from sortedcontainers import SortedDict
56+
57+
58+
class MyCalendarTwo:
59+
60+
def __init__(self):
61+
self.sd = SortedDict()
62+
63+
def book(self, start: int, end: int) -> bool:
64+
if start not in self.sd:
65+
self.sd[start] = 0
66+
if end not in self.sd:
67+
self.sd[end] = 0
68+
self.sd[start] += 1
69+
self.sd[end] -= 1
70+
s = 0
71+
for v in self.sd.values():
72+
s += v
73+
if s > 2:
74+
self.sd[start] -= 1
75+
self.sd[end] += 1
76+
return False
77+
return True
78+
79+
80+
# Your MyCalendarTwo object will be instantiated and called as such:
81+
# obj = MyCalendarTwo()
82+
# param_1 = obj.book(start,end)
5683
```
5784

5885
### **Java**
5986

6087
```java
6188
class MyCalendarTwo {
62-
List<int[]> calendar;
63-
List<int[]> duplicationList;
89+
private Map<Integer, Integer> tm = new TreeMap<>();
6490

65-
MyCalendarTwo() {
66-
calendar = new ArrayList<>();
67-
duplicationList = new ArrayList<>();
68-
}
91+
public MyCalendarTwo() {
6992

93+
}
94+
7095
public boolean book(int start, int end) {
71-
for (int[] item : duplicationList) {
72-
if (item[0] < end && item[1] > start) {
96+
tm.put(start, tm.getOrDefault(start, 0) + 1);
97+
tm.put(end, tm.getOrDefault(end, 0) - 1);
98+
int s = 0;
99+
for (int v : tm.values()) {
100+
s += v;
101+
if (s > 2) {
102+
tm.put(start, tm.get(start) - 1);
103+
tm.put(end, tm.get(end) + 1);
73104
return false;
74105
}
75106
}
76-
for (int[] item : calendar) {
77-
if (item[0] < end && item[1] > start) {
78-
duplicationList.add(new int[]{Math.max(start, item[0]), Math.min(end, item[1])});
107+
return true;
108+
}
109+
}
110+
111+
/**
112+
* Your MyCalendarTwo object will be instantiated and called as such:
113+
* MyCalendarTwo obj = new MyCalendarTwo();
114+
* boolean param_1 = obj.book(start,end);
115+
*/
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class MyCalendarTwo {
122+
public:
123+
map<int, int> m;
124+
125+
MyCalendarTwo() {
126+
127+
}
128+
129+
bool book(int start, int end) {
130+
++m[start];
131+
--m[end];
132+
int s = 0;
133+
for (auto& [k, v] : m)
134+
{
135+
s += v;
136+
if (s > 2)
137+
{
138+
--m[start];
139+
++m[end];
140+
return false;
79141
}
80142
}
81-
calendar.add(new int[]{start, end});
82143
return true;
83144
}
84-
}
145+
};
146+
147+
/**
148+
* Your MyCalendarTwo object will be instantiated and called as such:
149+
* MyCalendarTwo* obj = new MyCalendarTwo();
150+
* bool param_1 = obj->book(start,end);
151+
*/
85152
```
86153
87154
### **...**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class MyCalendarTwo {
2+
public:
3+
map<int, int> m;
4+
5+
MyCalendarTwo() {
6+
7+
}
8+
9+
bool book(int start, int end) {
10+
++m[start];
11+
--m[end];
12+
int s = 0;
13+
for (auto& [k, v] : m)
14+
{
15+
s += v;
16+
if (s > 2)
17+
{
18+
--m[start];
19+
++m[end];
20+
return false;
21+
}
22+
}
23+
return true;
24+
}
25+
};
26+
27+
/**
28+
* Your MyCalendarTwo object will be instantiated and called as such:
29+
* MyCalendarTwo* obj = new MyCalendarTwo();
30+
* bool param_1 = obj->book(start,end);
31+
*/
Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
class MyCalendarTwo {
2-
List<int[]> calendar;
3-
List<int[]> duplicationList;
2+
private Map<Integer, Integer> tm = new TreeMap<>();
43

5-
MyCalendarTwo() {
6-
calendar = new ArrayList<>();
7-
duplicationList = new ArrayList<>();
8-
}
4+
public MyCalendarTwo() {
95

6+
}
7+
108
public boolean book(int start, int end) {
11-
for (int[] item : duplicationList) {
12-
if (item[0] < end && item[1] > start) {
9+
tm.put(start, tm.getOrDefault(start, 0) + 1);
10+
tm.put(end, tm.getOrDefault(end, 0) - 1);
11+
int s = 0;
12+
for (int v : tm.values()) {
13+
s += v;
14+
if (s > 2) {
15+
tm.put(start, tm.get(start) - 1);
16+
tm.put(end, tm.get(end) + 1);
1317
return false;
1418
}
1519
}
16-
for (int[] item : calendar) {
17-
if (item[0] < end && item[1] > start) {
18-
duplicationList.add(new int[]{Math.max(start, item[0]), Math.min(end, item[1])});
19-
}
20-
}
21-
calendar.add(new int[]{start, end});
2220
return true;
2321
}
2422
}
23+
24+
/**
25+
* Your MyCalendarTwo object will be instantiated and called as such:
26+
* MyCalendarTwo obj = new MyCalendarTwo();
27+
* boolean param_1 = obj.book(start,end);
28+
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from sortedcontainers import SortedDict
2+
3+
4+
class MyCalendarTwo:
5+
6+
def __init__(self):
7+
self.sd = SortedDict()
8+
9+
def book(self, start: int, end: int) -> bool:
10+
if start not in self.sd:
11+
self.sd[start] = 0
12+
if end not in self.sd:
13+
self.sd[end] = 0
14+
self.sd[start] += 1
15+
self.sd[end] -= 1
16+
s = 0
17+
for v in self.sd.values():
18+
s += v
19+
if s > 2:
20+
self.sd[start] -= 1
21+
self.sd[end] += 1
22+
return False
23+
return True
24+
25+
26+
# Your MyCalendarTwo object will be instantiated and called as such:
27+
# obj = MyCalendarTwo()
28+
# param_1 = obj.book(start,end)

0 commit comments

Comments
 (0)