diff --git a/solution/0700-0799/0731.My Calendar II/README.md b/solution/0700-0799/0731.My Calendar II/README.md
index 1971961290950..578746ebaf9a2 100644
--- a/solution/0700-0799/0731.My Calendar II/README.md
+++ b/solution/0700-0799/0731.My Calendar II/README.md
@@ -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
-解释:
+解释:
前两个日程安排可以添加至日历中。 第三个日程安排会导致双重预订,但可以添加至日历中。
第四个日程安排活动(5,15)不能添加至日历中,因为它会导致三重预订。
第五个日程安排(5,10)可以添加至日历中,因为它未使用已经双重预订的时间10。
@@ -642,4 +642,74 @@ func (this *MyCalendarTwo) Book(start int, end int) bool {
+
+
+### Solution 3: Line Sweep
+
+
+
+#### TypeScript
+
+```ts
+class MyCalendarTwo {
+ #OVERLAPS = 2;
+ #cnt: Record = {};
+
+ 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;
+ }
+}
+```
+
+
+
+
+
diff --git a/solution/0700-0799/0731.My Calendar II/README_EN.md b/solution/0700-0799/0731.My Calendar II/README_EN.md
index 5d3abb9bcf791..32a758cc70e09 100644
--- a/solution/0700-0799/0731.My Calendar II/README_EN.md
+++ b/solution/0700-0799/0731.My Calendar II/README_EN.md
@@ -46,9 +46,9 @@ tags:
Explanation
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.
@@ -624,4 +624,74 @@ func (this *MyCalendarTwo) Book(start int, end int) bool {
+
+
+### Solution 3: Line Sweep
+
+
+
+#### TypeScript
+
+```ts
+class MyCalendarTwo {
+ #OVERLAPS = 2;
+ #cnt: Record = {};
+
+ 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;
+ }
+}
+```
+
+
+
+
+
diff --git a/solution/0700-0799/0731.My Calendar II/Solution3.js b/solution/0700-0799/0731.My Calendar II/Solution3.js
new file mode 100644
index 0000000000000..86fcb55a25ca9
--- /dev/null
+++ b/solution/0700-0799/0731.My Calendar II/Solution3.js
@@ -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;
+ }
+}
diff --git a/solution/0700-0799/0731.My Calendar II/Solution3.ts b/solution/0700-0799/0731.My Calendar II/Solution3.ts
new file mode 100644
index 0000000000000..29d9836756197
--- /dev/null
+++ b/solution/0700-0799/0731.My Calendar II/Solution3.ts
@@ -0,0 +1,25 @@
+class MyCalendarTwo {
+ #OVERLAPS = 2;
+ #cnt: Record = {};
+
+ 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;
+ }
+}