Skip to content

feat: add solutions to lc problem: No.0731 #3575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 71 additions & 1 deletion solution/0700-0799/0731.My Calendar II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ MyCalendar.book(10, 40); // returns true
MyCalendar.book(5, 15); // returns false
MyCalendar.book(5, 10); // returns true
MyCalendar.book(25, 55); // returns true
<strong>解释:</strong>
<strong>解释:</strong>
前两个日程安排可以添加至日历中。 第三个日程安排会导致双重预订,但可以添加至日历中。
第四个日程安排活动(5,15)不能添加至日历中,因为它会导致三重预订。
第五个日程安排(5,10)可以添加至日历中,因为它未使用已经双重预订的时间10。
Expand Down Expand Up @@ -642,4 +642,74 @@ func (this *MyCalendarTwo) Book(start int, end int) bool {

<!-- solution:end -->

<!-- solution:start -->

### Solution 3: Line Sweep

<!-- tabs:start -->

#### TypeScript

```ts
class MyCalendarTwo {
#OVERLAPS = 2;
#cnt: Record<number, number> = {};

book(start: number, end: number): boolean {
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;

let sum = 0;
for (const v of Object.values(this.#cnt)) {
sum += v;
if (sum > this.#OVERLAPS) {
this.#cnt[start]--;
this.#cnt[end]++;

if (!this.#cnt[start]) delete this.#cnt[start];
if (!this.#cnt[end]) delete this.#cnt[end];

return false;
}
}

return true;
}
}
```

#### JavaScript

```js
class MyCalendarTwo {
#OVERLAPS = 2;
#cnt = {};

book(start, end) {
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;

let sum = 0;
for (const v of Object.values(this.#cnt)) {
sum += v;
if (sum > this.#OVERLAPS) {
this.#cnt[start]--;
this.#cnt[end]++;

if (!this.#cnt[start]) delete this.#cnt[start];
if (!this.#cnt[end]) delete this.#cnt[end];

return false;
}
}

return true;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
76 changes: 73 additions & 3 deletions solution/0700-0799/0731.My Calendar II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ tags:

<strong>Explanation</strong>
MyCalendarTwo myCalendarTwo = new MyCalendarTwo();
myCalendarTwo.book(10, 20); // return True, The event can be booked.
myCalendarTwo.book(50, 60); // return True, The event can be booked.
myCalendarTwo.book(10, 40); // return True, The event can be double booked.
myCalendarTwo.book(10, 20); // return True, The event can be booked.
myCalendarTwo.book(50, 60); // return True, The event can be booked.
myCalendarTwo.book(10, 40); // return True, The event can be double booked.
myCalendarTwo.book(5, 15); // return False, The event cannot be booked, because it would result in a triple booking.
myCalendarTwo.book(5, 10); // return True, The event can be booked, as it does not use time 10 which is already double booked.
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.
Expand Down Expand Up @@ -624,4 +624,74 @@ func (this *MyCalendarTwo) Book(start int, end int) bool {

<!-- solution:end -->

<!-- solution:start -->

### Solution 3: Line Sweep

<!-- tabs:start -->

#### TypeScript

```ts
class MyCalendarTwo {
#OVERLAPS = 2;
#cnt: Record<number, number> = {};

book(start: number, end: number): boolean {
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;

let sum = 0;
for (const v of Object.values(this.#cnt)) {
sum += v;
if (sum > this.#OVERLAPS) {
this.#cnt[start]--;
this.#cnt[end]++;

if (!this.#cnt[start]) delete this.#cnt[start];
if (!this.#cnt[end]) delete this.#cnt[end];

return false;
}
}

return true;
}
}
```

#### JavaScript

```js
class MyCalendarTwo {
#OVERLAPS = 2;
#cnt = {};

book(start, end) {
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;

let sum = 0;
for (const v of Object.values(this.#cnt)) {
sum += v;
if (sum > this.#OVERLAPS) {
this.#cnt[start]--;
this.#cnt[end]++;

if (!this.#cnt[start]) delete this.#cnt[start];
if (!this.#cnt[end]) delete this.#cnt[end];

return false;
}
}

return true;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
25 changes: 25 additions & 0 deletions solution/0700-0799/0731.My Calendar II/Solution3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class MyCalendarTwo {
#OVERLAPS = 2;
#cnt = {};

book(start, end) {
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;

let sum = 0;
for (const v of Object.values(this.#cnt)) {
sum += v;
if (sum > this.#OVERLAPS) {
this.#cnt[start]--;
this.#cnt[end]++;

if (!this.#cnt[start]) delete this.#cnt[start];
if (!this.#cnt[end]) delete this.#cnt[end];

return false;
}
}

return true;
}
}
25 changes: 25 additions & 0 deletions solution/0700-0799/0731.My Calendar II/Solution3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class MyCalendarTwo {
#OVERLAPS = 2;
#cnt: Record<number, number> = {};

book(start: number, end: number): boolean {
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;

let sum = 0;
for (const v of Object.values(this.#cnt)) {
sum += v;
if (sum > this.#OVERLAPS) {
this.#cnt[start]--;
this.#cnt[end]++;

if (!this.#cnt[start]) delete this.#cnt[start];
if (!this.#cnt[end]) delete this.#cnt[end];

return false;
}
}

return true;
}
}
Loading