Skip to content

Commit 3554dfe

Browse files
committed
feat: add solutions to lc problem: No.0731
1 parent 06eab06 commit 3554dfe

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。
@@ -642,4 +642,74 @@ func (this *MyCalendarTwo) Book(start int, end int) bool {
642642

643643
<!-- solution:end -->
644644

645+
<!-- solution:start -->
646+
647+
### Solution 3: Line Sweep
648+
649+
<!-- tabs:start -->
650+
651+
#### TypeScript
652+
653+
```ts
654+
class MyCalendarTwo {
655+
#OVERLAPS = 2;
656+
#cnt: Record<number, number> = {};
657+
658+
book(start: number, end: number): boolean {
659+
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
660+
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;
661+
662+
let sum = 0;
663+
for (const v of Object.values(this.#cnt)) {
664+
sum += v;
665+
if (sum > this.#OVERLAPS) {
666+
this.#cnt[start]--;
667+
this.#cnt[end]++;
668+
669+
if (!this.#cnt[start]) delete this.#cnt[start];
670+
if (!this.#cnt[end]) delete this.#cnt[end];
671+
672+
return false;
673+
}
674+
}
675+
676+
return true;
677+
}
678+
}
679+
```
680+
681+
#### JavaScript
682+
683+
```js
684+
class MyCalendarTwo {
685+
#OVERLAPS = 2;
686+
#cnt = {};
687+
688+
book(start, end) {
689+
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
690+
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;
691+
692+
let sum = 0;
693+
for (const v of Object.values(this.#cnt)) {
694+
sum += v;
695+
if (sum > this.#OVERLAPS) {
696+
this.#cnt[start]--;
697+
this.#cnt[end]++;
698+
699+
if (!this.#cnt[start]) delete this.#cnt[start];
700+
if (!this.#cnt[end]) delete this.#cnt[end];
701+
702+
return false;
703+
}
704+
}
705+
706+
return true;
707+
}
708+
}
709+
```
710+
711+
<!-- tabs:end -->
712+
713+
<!-- solution:end -->
714+
645715
<!-- 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.
@@ -624,4 +624,74 @@ func (this *MyCalendarTwo) Book(start int, end int) bool {
624624

625625
<!-- solution:end -->
626626

627+
<!-- solution:start -->
628+
629+
### Solution 3: Line Sweep
630+
631+
<!-- tabs:start -->
632+
633+
#### TypeScript
634+
635+
```ts
636+
class MyCalendarTwo {
637+
#OVERLAPS = 2;
638+
#cnt: Record<number, number> = {};
639+
640+
book(start: number, end: number): boolean {
641+
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
642+
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;
643+
644+
let sum = 0;
645+
for (const v of Object.values(this.#cnt)) {
646+
sum += v;
647+
if (sum > this.#OVERLAPS) {
648+
this.#cnt[start]--;
649+
this.#cnt[end]++;
650+
651+
if (!this.#cnt[start]) delete this.#cnt[start];
652+
if (!this.#cnt[end]) delete this.#cnt[end];
653+
654+
return false;
655+
}
656+
}
657+
658+
return true;
659+
}
660+
}
661+
```
662+
663+
#### JavaScript
664+
665+
```js
666+
class MyCalendarTwo {
667+
#OVERLAPS = 2;
668+
#cnt = {};
669+
670+
book(start, end) {
671+
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
672+
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;
673+
674+
let sum = 0;
675+
for (const v of Object.values(this.#cnt)) {
676+
sum += v;
677+
if (sum > this.#OVERLAPS) {
678+
this.#cnt[start]--;
679+
this.#cnt[end]++;
680+
681+
if (!this.#cnt[start]) delete this.#cnt[start];
682+
if (!this.#cnt[end]) delete this.#cnt[end];
683+
684+
return false;
685+
}
686+
}
687+
688+
return true;
689+
}
690+
}
691+
```
692+
693+
<!-- tabs:end -->
694+
695+
<!-- solution:end -->
696+
627697
<!-- 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)