Skip to content

Commit 6612058

Browse files
authored
feat: add java solution to lc problem: NO.0731. My Calendar II (doocs#550)
1 parent 5838e46 commit 6612058

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,30 @@ MyCalendar.book(25, 55); // returns true
6464
<!-- 这里可写当前语言的特殊实现逻辑 -->
6565

6666
```java
67-
67+
class MyCalendarTwo {
68+
List<int[]> calendar;
69+
List<int[]> duplicationList;
70+
71+
MyCalendarTwo() {
72+
calendar = new ArrayList<>();
73+
duplicationList = new ArrayList<>();
74+
}
75+
76+
public boolean book(int start, int end) {
77+
for (int[] item : duplicationList) {
78+
if (item[0] < end && item[1] > start) {
79+
return false;
80+
}
81+
}
82+
for (int[] item : calendar) {
83+
if (item[0] < end && item[1] > start) {
84+
duplicationList.add(new int[]{Math.max(start, item[0]), Math.min(end, item[1])});
85+
}
86+
}
87+
calendar.add(new int[]{start, end});
88+
return true;
89+
}
90+
}
6891
```
6992

7093
### **...**

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,30 @@ the time [40, 50) will be single booked, and the time [50, 55) will be double bo
8888
### **Java**
8989

9090
```java
91-
91+
class MyCalendarTwo {
92+
List<int[]> calendar;
93+
List<int[]> duplicationList;
94+
95+
MyCalendarTwo() {
96+
calendar = new ArrayList<>();
97+
duplicationList = new ArrayList<>();
98+
}
99+
100+
public boolean book(int start, int end) {
101+
for (int[] item : duplicationList) {
102+
if (item[0] < end && item[1] > start) {
103+
return false;
104+
}
105+
}
106+
for (int[] item : calendar) {
107+
if (item[0] < end && item[1] > start) {
108+
duplicationList.add(new int[]{Math.max(start, item[0]), Math.min(end, item[1])});
109+
}
110+
}
111+
calendar.add(new int[]{start, end});
112+
return true;
113+
}
114+
}
92115
```
93116

94117
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class MyCalendarTwo {
2+
List<int[]> calendar;
3+
List<int[]> duplicationList;
4+
5+
MyCalendarTwo() {
6+
calendar = new ArrayList<>();
7+
duplicationList = new ArrayList<>();
8+
}
9+
10+
public boolean book(int start, int end) {
11+
for (int[] item : duplicationList) {
12+
if (item[0] < end && item[1] > start) {
13+
return false;
14+
}
15+
}
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});
22+
return true;
23+
}
24+
}

0 commit comments

Comments
 (0)