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 a0bf608

Browse files
authored
feat: add solutions to lc problem: No.0731 (doocs#3575)
1 parent 49d13b7 commit a0bf608

File tree

4 files changed

+194
-4
lines changed

4 files changed

+194
-4
lines changed

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

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ MyCalendar.book(10, 40); // returns true
4343
MyCalendar.book(5, 15); // returns false
4444
MyCalendar.book(5, 10); // returns true
4545
MyCalendar.book(25, 55); // returns true
46-
<strong>解释:</strong>
46+
<strong>解释:</strong>
4747
前两个日程安排可以添加至日历中。 第三个日程安排会导致双重预订,但可以添加至日历中。
4848
第四个日程安排活动(5,15)不能添加至日历中,因为它会导致三重预订。
4949
第五个日程安排(5,10)可以添加至日历中,因为它未使用已经双重预订的时间10。
@@ -716,4 +716,74 @@ func (this *MyCalendarTwo) Book(start int, end int) bool {
716716

717717
<!-- solution:end -->
718718

719+
<!-- solution:start -->
720+
721+
### Solution 3: Line Sweep
722+
723+
<!-- tabs:start -->
724+
725+
#### TypeScript
726+
727+
```ts
728+
class MyCalendarTwo {
729+
#OVERLAPS = 2;
730+
#cnt: Record<number, number> = {};
731+
732+
book(start: number, end: number): boolean {
733+
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
734+
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;
735+
736+
let sum = 0;
737+
for (const v of Object.values(this.#cnt)) {
738+
sum += v;
739+
if (sum > this.#OVERLAPS) {
740+
this.#cnt[start]--;
741+
this.#cnt[end]++;
742+
743+
if (!this.#cnt[start]) delete this.#cnt[start];
744+
if (!this.#cnt[end]) delete this.#cnt[end];
745+
746+
return false;
747+
}
748+
}
749+
750+
return true;
751+
}
752+
}
753+
```
754+
755+
#### JavaScript
756+
757+
```js
758+
class MyCalendarTwo {
759+
#OVERLAPS = 2;
760+
#cnt = {};
761+
762+
book(start, end) {
763+
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
764+
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;
765+
766+
let sum = 0;
767+
for (const v of Object.values(this.#cnt)) {
768+
sum += v;
769+
if (sum > this.#OVERLAPS) {
770+
this.#cnt[start]--;
771+
this.#cnt[end]++;
772+
773+
if (!this.#cnt[start]) delete this.#cnt[start];
774+
if (!this.#cnt[end]) delete this.#cnt[end];
775+
776+
return false;
777+
}
778+
}
779+
780+
return true;
781+
}
782+
}
783+
```
784+
785+
<!-- tabs:end -->
786+
787+
<!-- solution:end -->
788+
719789
<!-- problem:end -->

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

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ tags:
4646

4747
<strong>Explanation</strong>
4848
MyCalendarTwo myCalendarTwo = new MyCalendarTwo();
49-
myCalendarTwo.book(10, 20); // return True, The event can be booked.
50-
myCalendarTwo.book(50, 60); // return True, The event can be booked.
51-
myCalendarTwo.book(10, 40); // return True, The event can be double booked.
49+
myCalendarTwo.book(10, 20); // return True, The event can be booked.
50+
myCalendarTwo.book(50, 60); // return True, The event can be booked.
51+
myCalendarTwo.book(10, 40); // return True, The event can be double booked.
5252
myCalendarTwo.book(5, 15); // return False, The event cannot be booked, because it would result in a triple booking.
5353
myCalendarTwo.book(5, 10); // return True, The event can be booked, as it does not use time 10 which is already double booked.
5454
myCalendarTwo.book(25, 55); // return True, The event can be booked, as the time in [25, 40) will be double booked with the third event, the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.
@@ -698,4 +698,74 @@ func (this *MyCalendarTwo) Book(start int, end int) bool {
698698

699699
<!-- solution:end -->
700700

701+
<!-- solution:start -->
702+
703+
### Solution 3: Line Sweep
704+
705+
<!-- tabs:start -->
706+
707+
#### TypeScript
708+
709+
```ts
710+
class MyCalendarTwo {
711+
#OVERLAPS = 2;
712+
#cnt: Record<number, number> = {};
713+
714+
book(start: number, end: number): boolean {
715+
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
716+
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;
717+
718+
let sum = 0;
719+
for (const v of Object.values(this.#cnt)) {
720+
sum += v;
721+
if (sum > this.#OVERLAPS) {
722+
this.#cnt[start]--;
723+
this.#cnt[end]++;
724+
725+
if (!this.#cnt[start]) delete this.#cnt[start];
726+
if (!this.#cnt[end]) delete this.#cnt[end];
727+
728+
return false;
729+
}
730+
}
731+
732+
return true;
733+
}
734+
}
735+
```
736+
737+
#### JavaScript
738+
739+
```js
740+
class MyCalendarTwo {
741+
#OVERLAPS = 2;
742+
#cnt = {};
743+
744+
book(start, end) {
745+
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
746+
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;
747+
748+
let sum = 0;
749+
for (const v of Object.values(this.#cnt)) {
750+
sum += v;
751+
if (sum > this.#OVERLAPS) {
752+
this.#cnt[start]--;
753+
this.#cnt[end]++;
754+
755+
if (!this.#cnt[start]) delete this.#cnt[start];
756+
if (!this.#cnt[end]) delete this.#cnt[end];
757+
758+
return false;
759+
}
760+
}
761+
762+
return true;
763+
}
764+
}
765+
```
766+
767+
<!-- tabs:end -->
768+
769+
<!-- solution:end -->
770+
701771
<!-- problem:end -->
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class MyCalendarTwo {
2+
#OVERLAPS = 2;
3+
#cnt = {};
4+
5+
book(start, end) {
6+
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
7+
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;
8+
9+
let sum = 0;
10+
for (const v of Object.values(this.#cnt)) {
11+
sum += v;
12+
if (sum > this.#OVERLAPS) {
13+
this.#cnt[start]--;
14+
this.#cnt[end]++;
15+
16+
if (!this.#cnt[start]) delete this.#cnt[start];
17+
if (!this.#cnt[end]) delete this.#cnt[end];
18+
19+
return false;
20+
}
21+
}
22+
23+
return true;
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class MyCalendarTwo {
2+
#OVERLAPS = 2;
3+
#cnt: Record<number, number> = {};
4+
5+
book(start: number, end: number): boolean {
6+
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
7+
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;
8+
9+
let sum = 0;
10+
for (const v of Object.values(this.#cnt)) {
11+
sum += v;
12+
if (sum > this.#OVERLAPS) {
13+
this.#cnt[start]--;
14+
this.#cnt[end]++;
15+
16+
if (!this.#cnt[start]) delete this.#cnt[start];
17+
if (!this.#cnt[end]) delete this.#cnt[end];
18+
19+
return false;
20+
}
21+
}
22+
23+
return true;
24+
}
25+
}

0 commit comments

Comments
(0)

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